0

我已经将 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)); }
        }
4

1 回答 1

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。

于 2012-09-06T22:52:26.620 回答