c# - 如何设置 XAML 组合框的选定值?

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

最佳答案

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

关于c# - 如何设置 XAML 组合框的选定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1594444/

相关文章:

每个单元格具有不同数据源的 DataGridView 组合框

c# - 通过套接字在 C++ 和 C# 应用程序之间传输图像

c# - 在运行时即时更改应用程序语言

c# - 第二次尝试时 TcpClient 无法读取

c# - 一般执行 DropDownList 事件

c# - 动态更改DataTemplate的宽度

c# - 切片图像wpf

c# - 附加属性绑定(bind)不起作用

WPF 组合框验证

excel - 从excel vba用户表单组合框中选定行的列中提取数据