tags:

views:

889

answers:

3

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

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.

Noldorin
even when Salutation = "Mr." is before InitializeComponent the combobox is still blank :-(
Edward Tanguay
@Edward: Perhaps try binding `SelectedItem` instead of `SelectedValue` in XAML?
Noldorin
thanks but SelectedItem still leaves the ComboBox blank
Edward Tanguay
Hmm... what can I say? There's no reason at all that shouldn't work - I've done it numerous times - maybe there is some code elsewhere doing strange things. Anyway, try changing the binding `Mode` and just playing around with things.
Noldorin
I finally solved it by using SelectedIndex, but strange that SelectedItem and SelectedValue doesn't work in this case.
Edward Tanguay
A: 

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.

Bryan Anderson
I tried that, but the combobox is still empty (posted code above).
Edward Tanguay
Oh! Your items aren't strings. You need to use the ItemsSource property to use SelectedItem and/or SelectedValue. You might also be able to do it by using <ComboBoxItem><sys:String>Mr.</sys:String></ComboBoxItem> rather than just <ComboBoxItem>Mr.</ComboBoxItem>
Bryan Anderson
A: 

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

Edward Tanguay