How do I set the SelectedIndex of ComboBox on the C# side?

Asked 2 years ago, Updated 2 years ago, 295 views

When you create ComboBox on the C# side, you do not know how to set the SelectedIndex property.
What method should I use?

Please use the xaml below as an example.

<ComboBoxName="newCombo"
            SelectedIndex="{
        Binding someData, 
        Mode=TwoWay, 
        UpdateSourceTrigger=PropertyChanged}">
    <ComboBoxItem>A</ComboBoxItem>
    <ComboBoxItem>B</ComboBoxItem>
</ComboBox>

Currently, the C# code I have considered so far is as follows:

 varcmb= 
    newComboBox(){
        Name = "newCombo",
        SelectedIndex=someData,
    };

var binding= 
    new Binding
    {
        Mode=BindingMode.TwoWay,
        UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
    };

// I don't know myself from here.
// How do you incorporate binding into cmb?
// Or is it another way?
            

c# wpf xaml

2022-09-30 22:03

1 Answers

You can include binding in cmb with SetBinding.
Example: cmb.SetBinding (ComboBox.SelectedIndexProperty, binding);

The sample code below integrates the SelectedIndex of the combo box dynamically created by the WpfApp1 project with Text in the text box.

sample code

MainWindow.xaml.cs

using System.Collections.General;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace WpfApp1
{
    public partial class MainWindow—Window
    {
        MySelection mySelection;
        public MainWindow()
        {
            InitializeComponent();

            mySelection=new MySelection();
            // Binding Creation
            var binding = new Binding ("SelectedIndex")
            {
                Mode=BindingMode.TwoWay,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
                Source = mySelection,
            };
            // dynamic combo box creation
            varcmb = new ComboBox
            {
                Name = "newCombo",
                ItemsSource=newList<string>{"A", "B"},
            };

            // ☆ Set binding to SelectedInex in combo box ☆
            cmb.SetBinding (ComboBox.SelectedIndexProperty, binding);

            // Place dynamically created combo and text boxes on the C# side
            vargrid=(Content as Grid);
            grid.Children.Add(cmb);
            var text = new TextBox();
            text.SetBinding (TextBox.TextProperty, binding);
            Grid.SetColumn(text,2);
            grid.Children.Add(text);
        }
    }

    /// <summary>
    /// Change Notification Class (available in C#6.0 and later)
    /// </summary>
    public class MySelection:INotifyPropertyChanged
    {
        private int selectedIndex;

        public event PropertyChangedEventHandler?PropertyChanged;
        public int SelectedIndex
        {
            get {return selectedIndex;}
            set
            {
                selectedIndex=value;
                OnPropertyChanged ("SelectedIndex");
            }
        }

        protected void OnPropertyChanged ([CallerMemberName] string?info=null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
        }
    }
}

MainWindow.xaml

<Window:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns: x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns: mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc —Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="1"Text="SelectedIndex and right text work together"/>
    </Grid>
</Window>


2022-09-30 22:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.