How to set the selected XAML Combobox value?

Why does the following example show combobox instead of "Mr."?

XAML:

<Window x:Class="TestComb82822.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel HorizontalAlignment="Left">
        <ComboBox SelectedValue="{Binding Salutation}" Width="200">
            <ComboBoxItem>Company</ComboBoxItem>
            <ComboBoxItem>Ms.</ComboBoxItem>
            <ComboBoxItem>Mr.</ComboBoxItem>
        </ComboBox>
    </StackPanel>
</Window>

      

Code behind:

using System.Windows;
using System.ComponentModel;

namespace TestComb82822
{
    public partial class Window1 : Window, INotifyPropertyChanged
    {
        #region ViewModelProperty: Salutation
        private string _salutation;
        public string Salutation
        {
            get
            {
                return _salutation;
            }

            set
            {
                _salutation = value;
                OnPropertyChanged("Salutation");
            }
        }
        #endregion

        public Window1()
        {
            InitializeComponent();
            DataContext = this;
            Salutation = "Mr.";
        }

        #region INotifiedProperty Block
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

    }
}

      

Second try:

Bryan, SelectedItem and WindowLoaded don't work either, this makes the ComboBox empty anyway:

XAML:

<Window x:Class="TestCombo234.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel HorizontalAlignment="Left">
        <ComboBox SelectedItem="{Binding Salutation}" Width="200">
            <ComboBoxItem>Company</ComboBoxItem>
            <ComboBoxItem>Ms.</ComboBoxItem>
            <ComboBoxItem>Mr.</ComboBoxItem>
        </ComboBox>
    </StackPanel>
</Window>

      

Code for:

using System.Windows;
using System.ComponentModel;

namespace TestCombo234
{
    public partial class Window1 : Window, INotifyPropertyChanged
    {
        #region ViewModelProperty: Salutation
        private string _salutation;
        public string Salutation
        {
            get
            {
                return _salutation;
            }

            set
            {
                _salutation = value;
                OnPropertyChanged("Salutation");
            }
        }
        #endregion

        public Window1()
        {
            InitializeComponent();
            DataContext = this;
            Loaded += new RoutedEventHandler(Window1_Loaded);
        }

        void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            Salutation = "Mr.";
        }

        #region INotifiedProperty Block
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}

      

+2


source to share


4 answers


First you need to set SelectedItem instead of SelectedValue. Second, you are setting the SelectedItem before the ComboBox is actually set up, try setting the SelectedItem to the window loaded event.



+1


source


My solution in this case was to just use ItemIndex i.e. "Mr." = 2



+1


source


By default, a ComboBox

should actually select the first element. However, you are creating a binding SelectedValue

here, which is two-way by default. Moreover, on load, the value Salutation

(to which you are binding) actually remains null. Try to install Salutation = "Mr.";

before InitializeComponent

and you should be fine.

0


source


You are creating a List / Observable collection in the code behind.

public ObservableCollection<string> Collection
        {
            get; set;
        }

      

eg:

<ComboBox Name="NameCombo" ItemsSource="{Binding}">

      

then set the Observable collection as the datacontext for ComboBox in Window_Loaded method.

eg:

NameCombo.DataContext=Collection;

      

0


source







All Articles