C# WPF将ComboBox项源绑定到ObservableCollection,将SelectedItem绑定到DataContext模型属性

C# WPF将ComboBox项源绑定到ObservableCollection,将SelectedItem绑定到DataContext模型属性,c#,wpf,data-binding,C#,Wpf,Data Binding,我是WPF的新手,我很感兴趣这能不能做到。 我在主窗口中为combobox控件定义了一个字符串数据源,如下所示: public readonly ObservableCollection<string> _roles = new ObservableCollection<string>() { "User", "Admin", "Developer" }; 该模型在xaml中定义为我的DataContext

我是WPF的新手,我很感兴趣这能不能做到。 我在主窗口中为combobox控件定义了一个字符串数据源,如下所示:

public readonly ObservableCollection<string> _roles = new ObservableCollection<string>() { "User", "Admin", "Developer" };
该模型在xaml中定义为我的DataContext

在表单中,我有一个组合框,在其中绑定对象的Role属性。如果我在代码中设置combobox ItemSource属性,这将非常完美:

cbRoles.ItemsSource = _roles;
但是我很感兴趣,我能用xaml做这个吗? 这是我的组合框的代码片段

<StackPanel Grid.Row="2"
                    Margin="5"
                    Orientation="Vertical">
            <Label x:Name="lblRole" 
                   Content="Role" />
            <ComboBox ItemsSource="{Binding Path=Role,
                                    UpdateSourceTrigger=PropertyChanged,
                                    NotifyOnValidationError=True,
                                    ValidatesOnDataErrors=True}"
                 DisplayMemberPath="{Binding SimpleStringProperty}"
                 SelectedValuePath="{Binding SimpleStringProperty}"
                 SelectedItem="{Binding SimpleStringProperty}"
                 IsSynchronizedWithCurrentItem="True" 
                 Text="Select Option"
                 Margin="1"
                 Height="20"
                 x:Name="cbRoles"/>
        </StackPanel>

要从XAML设置组合框.ItemsSource,必须使用数据绑定。要使用数据绑定,源必须是
公共
属性,这需要将
\u角色
字段转换为
公共
属性

因为项目是纯字符串,所以它没有任何属性,因此没有路径。不能选择属性来提供显示值或选择值<代码>组合框。DisplayMemberPath和
组合框。必须删除SelectedValuePath

默认情况下,
Binding.UpdateSourceTrigger
设置为
UpdateSourceTrigger.PropertyChanged

LoginRequest
上不存在
SimpleStringProperty
SelectedItem
应该绑定到
LoginRequest.Role

MainWindow.xaml.cs

partial class MainWindow : Window
{
  public ObservableCollection<string> Roles { get; }

  public MainWindow()
  {
    InitializeComponent();
    this.Roles = new ObservableCollection<string>() { "User", "Admin", "Developer" };
  }
}
部分类主窗口:窗口
{
公共ObservableCollection角色{get;}
公共主窗口()
{
初始化组件();
this.Roles=newobserveCollection(){“User”、“Admin”、“Developer”};
}
}
main window.xaml

<ComboBox ItemsSource="{Binding Path=Roles, 
                                RelativeSource={RelativeSource AncestorType=Window}},
                                NotifyOnValidationError=True,
                                ValidatesOnDataErrors=True}"
          SelectedItem="{Binding Role}"
          IsSynchronizedWithCurrentItem="True" />

ItemsSource=“{Binding Path=Role…}”
当Role是一个字符串时,它似乎毫无意义。您是想将
SelectedItem
绑定到角色吗?还请注意,如果没有
SelectedValue
绑定或赋值,则设置
SelectedValuePath
无效。
<ComboBox ItemsSource="{Binding Path=Roles, 
                                RelativeSource={RelativeSource AncestorType=Window}},
                                NotifyOnValidationError=True,
                                ValidatesOnDataErrors=True}"
          SelectedItem="{Binding Role}"
          IsSynchronizedWithCurrentItem="True" />