Edward Tanguay
Edward Tanguay

Reputation: 193442

How can I set selected value of a XAML Combobox?

Why in the following example is the combobox set to blank 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 doesn't work either, this still makes the ComboBox blank:

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 Behind:

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
    }
}

Upvotes: 2

Views: 15457

Answers (4)

K.K.Srinivasan
K.K.Srinivasan

Reputation: 69

You do create 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 datacontext for ComboBox in the Window_Loaded method.

eg:

NameCombo.DataContext=Collection;

Upvotes: 0

Edward Tanguay
Edward Tanguay

Reputation: 193442

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

Upvotes: 2

Bryan Anderson
Bryan Anderson

Reputation: 16129

First you need to set SelectedItem instead of SelectedValue. Second, you're setting the SelectedItem before the ComboBox has actually been set up, try setting the SelectedItem in the Window's loaded event.

Upvotes: 1

Noldorin
Noldorin

Reputation: 147461

By default, a ComboBox should indeed select it's first item. However, you are creating a binding of the SelectedValue here, which is by default two-way. What is more, on load, the value of Salutation (to which you are binding) is actually still null. Try setting Salutation = "Mr."; before InitializeComponent and all should be fine.

Upvotes: 0

Related Questions