Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone, I have a listview with some checkboxes. I want to be able to populate a DataGridCombobox Itemssource based on the checked selection in the ListView.

What I have tried:

My Model is Order with only 1 property, OrderNumber. In the ViewModel I initialized:

ObservableCollection<order> OrderNumbers will populate the ListView filled with checkboxes.

List<order> SelectedOrders will be a list of selected orders from the said ListView. The application will scan all the ListViewItems for IsChecked Property when an item IsChecked Property changes, adding only items where IsChecked = true to this list.

ObservableCollection<order> SelectedOrderNumbers will be the DataGridItems

C#
OrderNumbers = new ObservableCollection<Order>();
OrderNumbers.Add(new Order("Order No.1"));
OrderNumbers.Add(new Order("Order No.2"));
OrderNumbers.Add(new Order("Order No.3"));

SelectedOrders = new List<Order>();

SelectedOrderNumbers = new ObservableCollection<Order>();


And this is the view:
XML
<Grid>
   <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>

        <ListView ItemsSourche="{Binding OrderNumbers}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="False" Content="{Binding OrderNumber}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <DataGrid Grid.Column="1" ItemsSource="{Binding SelectedOrderNumbers}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridComboBoxColumn Header="Order Number" Width="150">
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="ItemsSource" Value="{}"/>
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                    <DataGridComboBoxColumn.ElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="ItemsSource" Value="{}"/>
                        </Style>
                    </DataGridComboBoxColumn.ElementStyle>
                </DataGridComboBoxColumn>
    </DataGrid>
</Grid>
Posted
Comments
[no name] 13-Feb-21 6:27am    
You're trying to template a CheckBox onto a datasource that has no boolean member.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900