user1584245
user1584245

Reputation:

How to bind ObservableCollection in DataGridComboBoxColumn in DataGrid?

I have already given ListCollectionView as "ListCollectionView1" to Grid and inGrid I have used DataGrid. "ListCollectionView1" contains both observable collection. "ObservableCollection1" DataGrid in and another observable collection as "ObservableCollection2" which is in "ObservableCollection1" observable collection as Itemsource to DataGridComboBoxColumn . And as SelectedValueBinding I'm using Property as "a" from "ObservableCollection1" but I'm not getting the values in 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>

in my viewModel observable collections and listcollectionview are

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)); }
        }

Upvotes: 1

Views: 1925

Answers (1)

123 456 789 0
123 456 789 0

Reputation: 10865

Try this.

 <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>

Add the DataContext in your style binding instead of just the property name. You are just setting the ItemsSource of the ColumnBoxHeader which doesn't make sense. It doesn't know how to populate the ComboBox where it is in the children of the control.

Upvotes: 1

Related Questions