DataGridComboboxColumn Binding with ObservableCollection

本文关键字:ObservableCollection with Binding DataGridComboboxColumn | 更新日期: 2023-09-27 17:57:41

我的小项目现在看起来完全不同了。现在我已经ObservableCollection OC1了我想与整个DatagridTextBoxColumns绑定的数据(通过绑定路径(,并ObservableCollection OC2我存储DatagridComboboxColumn案例的位置(如ItemsSourceBinding(。 DatagridComboboxColumn 中的SelectedItem属性是 OC1 中的一个值(它是 ofc OC2情况之一(。绑定DatagridTextBoxColumns是可以的。

XAML:

<DataGrid x:Name="DGoc1" x:Uid="DGoc1" AutoGenerateColumns="False"
          AlternationCount="2" SelectionMode="Single" Margin="0,5,0,0" 
          HorizontalAlignment="Stretch">
  <DataGrid.Columns>
    <!-- This works fine -->
    <DataGridTextColumn Binding="{Binding Path=id}" Header="ID" 
                        Width="Auto" IsReadOnly="True"/>
    <!-- Dow to bind this properly?? -->
    <DataGridComboBoxColumn ItemsSource="{Binding OC2}" 
                            SelectedItemBinding="{Binding Path=valueFromOc1}"
                            Header="OC2Cases" Width="Auto"/>

C#(更新(:

public class ClasswithSomeData
{
    public int id { get; set; }
    public string valueFromOc1 { get; set; }
}
public partial class DGCBC : Window
{
    public ObservableCollection<string> OC2 { get; set; }
    public ObservableCollection<ClasswithSomeData> OC1 { get; set;}
    private void tabPanel1_Loaded(object sender, RoutedEventArgs e)
    {
        LoadDG();
    }
public void LoadDG()
    {
    OC2 = new ObservableCollection<string>(someCases);
    OC1 = new ObservableCollection<ClasswithSomeData> { };
    OC1.Add(someData1);
    OC1.Add(someData2);
    OC1.Add(someData3);
    DGoc1.ItemsSource = OC1;

如何正确绑定此DatagridComboboxColumn?请帮助一些例子。

DataGridComboboxColumn Binding with ObservableCollection

您的可观察集合声明为字段:

public ObservableCollection<string> OC2;
public ObservableCollection<ClasswithSomeData> OC1;

它们应该是属性:

public ObservableCollection<string> OC2 {get;set;}
public ObservableCollection<ClasswithSomeData> {get;set;} 

不要忘记InotifyPropertyChanged! =(