【问题标题】:How to bind ObservableCollection in DataGridComboBoxColumn in DataGrid?如何在 DataGrid 的 DataGridComboBoxColumn 中绑定 ObservableCollection?
【发布时间】:2017-10-16 10:47:57
【问题描述】:

我已经将 ListCollectionView 作为“ListCollectionView1”提供给 Grid 和 inGrid,我使用了 DataGrid。 “ListCollectionView1”包含两个可观察的集合。 “ObservableCollection1” DataGrid 和另一个可观察集合作为“ObservableCollection2”,它在“ObservableCollection1”可观察集合中作为 Itemsource 到 DataGridComboBoxColumn 。 并且作为 SelectedValueBinding 我使用 Property as "a" from "ObservableCollection1" 但我没有得到 DataGridComboBoxColumn 中的值

 <Grid DockPanel.Dock="Bottom" DataContext="{Binding ListCollectionView1>
<DataGrid 
                                ColumnWidth="130"
                                CanUserAddRows="True"
                                AutoGenerateColumns="False"
                                ItemContainerStyle="{StaticResource DataGridRowContentStyle}"
                                ItemsSource="{Binding ObservableCollection1 }" 
                                CanUserDeleteRows="False">
                        <DataGridComboBoxColumn Header="Labour" ItemsSource="{Binding Path=ObservableCollection2 , RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"  SelectedValueBinding="{Binding Id}" SelectedValuePath="Id" DisplayMemberPath="Id" HeaderStyle="{StaticResource DataGridHeaderStyle}"/>
</DataGrid>
</Grid>

在我的 viewModel 中可观察的集合和 listcollectionview 是

private ListCollectionView _ListCollectionView1;
        public ListCollectionView ListCollectionView1
        {
            get { return _ListCollectionView1; }
            set
            {
                this._ListCollectionView1= value;
                OnPropertyChanged("ListCollectionView1");
            }
        }

public ObservableCollection<Model_ObservableCollection1> ModelObservableCollection1
        {
            get { return new ObservableCollection<Model_ObservableCollection1>(ViewModel.AllDataCollactions.AllTransactionsDetails.Where(s => s.TransactionsID.Equals(TransactionsID))); }
        }
        public ObservableCollection<Model_ObservableCollection2> Model_ObservableCollection1
        {
            get { return new ObservableCollection<Model_ObservableCollection1>(ViewModel.AllDataCollactions.AllTransactionsDetails.Where(s => s.TransactionsID.Equals(TransactionsID) && s.IsJama)); }
        }

【问题讨论】:

  • 从您的代码看来,ListCollectionView1 不包含两个可观察的集合,即ObservableCollection1ObservableCollection2
  • 没关系,他将源指向 ViewModel 的 UserControl。

标签: wpf xaml data-binding


【解决方案1】:

试试这个。

 <DataGrid x:Name="testGrid" AutoGenerateColumns="True" ItemsSource="{Binding ObservableCollection}" >
        <DataGrid.Columns>
            <DataGridComboBoxColumn Header="Labour"  DisplayMemberPath="test">
                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="{x:Type ComboBox}">
                        <Setter Property="ItemsSource" Value="{Binding Path=DataContext.ObservableCollection2 , RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="{x:Type ComboBox}">
                        <Setter Property="ItemsSource" Value="{Binding Path=DataContext.ObservableCollection2, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>
        </DataGrid.Columns>
    </DataGrid>

在您的样式绑定中添加 DataContext 而不仅仅是属性名称。您只是在设置没有意义的 ColumnBoxHeader 的 ItemsSource 。它不知道如何在控件的子控件中填充 ComboBox。

【讨论】:

  • 嘿 thnx 寻求帮助,但我自己已经成功完成了。我的错误是我的可观察集合未在 viewmodel 中正确设置..
猜你喜欢
  • 1970-01-01
  • 2013-04-24
  • 1970-01-01
  • 2017-08-05
  • 2013-03-30
  • 2015-01-21
  • 2014-01-04
  • 2011-08-03
  • 2013-12-12
相关资源
最近更新 更多