如何设置XAML Combobox的选定值?

时间:2009-10-20 12:59:10

标签: c# xaml combobox

为什么以下示例中的组合框设置为空白而不是“先生”?

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>

代码背后:

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

    }
}

第二次尝试:

Bryan,SelectedItem和WindowLoaded也不起作用,这仍然使ComboBox变为空白:

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>

代码背后:

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

4 个答案:

答案 0 :(得分:1)

首先,您需要设置SelectedItem而不是SelectedValue。其次,在实际设置ComboBox之前设置SelectedItem,尝试在Window加载的事件中设置SelectedItem。

答案 1 :(得分:1)

我在这种情况下的解决方案是使用ItemIndex,即“Mr.” = 2

答案 2 :(得分:0)

默认情况下,ComboBox确实应该选择它的第一项。但是,您正在此处创建SelectedValue的绑定,默认情况下为双向。更重要的是,在加载时,Salutation(您绑定到的)的值实际上仍为空。尝试在 Salutation = "Mr.";之前设置InitializeComponent ,一切都应该没问题。

答案 3 :(得分:0)

您可以在后面的代码中创建List / Observable集合。

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

例如:

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

然后在Window_Loaded方法中将Observable集合设置为ComboBox的datacontext。

例如:

NameCombo.DataContext=Collection;