1

I am trying to do some simple thing in WPF but can't find the way to do it.

I have a ComboBox in a DataGrid header to filter data. The data is binded to a GrouBy statement of all my data.

These show some CheckBox The XAML code is:

<DataGridTextColumn x:Name="Type"
                    Binding="{Binding TypeOfData, Mode=OneTime}"
                    SortMemberPath="TypeOfData"
                    IsReadOnly="true"
                    CanUserSort="true">
    <DataGridTextColumn.Header>
        <DockPanel>
            <Label Content="Type Of Data"
                   DockPanel.Dock="Left"/>  
            <ComboBox x:Name="comboBoxType"
                      DockPanel.Dock="Right" 
                      SelectionChanged="comboBoxType_SelectionChanged">                         
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel x:Name="itemsComboBox">
                           <CheckBox Name="checkBoxType"
                                     IsChecked="False"
                                     Content="{Binding Key}"
                                     Unchecked="FilterChange" 
                                     Checked="FilterChange"/>
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
        </DockPanel>
    </DataGridTextColumn.Header>
</DataGridTextColumn>

The code behind fore binding is:

comboBoxType.ItemsSource = allData.GroupBy(data=> data.TypeOfData).OrderBy(grp=> grp.Key);

And this work.

But now I want to add 2 button to check and uncheck all at the end or at the start of the ComboBox but I can't seem to find how to add those static button in a dynamic data template.

Edit Answer to grek40: I want the buttons in the combobox Items before or after the checkboxes.

CompositeCollection could help but I can't seem to make it work.

Thank You

AwkwardCoder
  • 24,893
  • 27
  • 82
  • 152
user3704628
  • 143
  • 1
  • 12
  • Do ItemsSource={Binding ...} and create the ObservableCollection property in the code behind to be bound. – kurakura88 Jun 06 '17 at 01:40
  • Bind `IsChecked` to your ViewModel and do it in code. – Manfred Radlwimmer Jun 06 '17 at 06:19
  • Can you be more specific where you want the buttons? It sounds like you want them inside the combobox dropdown and thats possible but might get a bit complicated. – grek40 Jun 06 '17 at 06:25
  • @kurakura88 I don't understand your comment. how does that help me to add some static button that always appear in the combobox item independent from my binding? – user3704628 Jun 06 '17 at 11:00
  • @grek40 I updated my question. I know some part of the solution could be in CompositeCollection but I can't get it to work at all. – user3704628 Jun 06 '17 at 11:01
  • To me it looks as if you use the `ComboBox` as a bad replacement for a popup, since you don't actually select a single item from a list (that's what `ComboBox` is for) but you want to manage multiple selectable items... I'd say, use a `ToggleButton` to open/close the filter popup and a `Popup` to hold the list of checkboxes and the additional buttons. – grek40 Jun 06 '17 at 11:07

1 Answers1

0

Replace the DockPanel with a Grid that has several ColumnDefinitions:

<DataGridTextColumn x:Name="Type" Binding="{Binding TypeOfData, Mode=OneTime}" SortMemberPath="TypeOfData" IsReadOnly="true" CanUserSort="true">
    <DataGridTextColumn.Header>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Label Content="Type Of Data"  />
            <ComboBox x:Name="comboBoxType"
                      Grid.Column="1"
                      SelectionChanged="comboBoxType_SelectionChanged">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel x:Name="itemsComboBox">
                            <CheckBox Name="checkBoxType"
                                      IsChecked="False"
                                      Content="{Binding Key}"
                                      Unchecked="FilterChange" 
                                      Checked="FilterChange"/>
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <Button Content="Check"
                    Grid.Column="2" />
        </Grid>
    </DataGridTextColumn.Header>
</DataGridTextColumn>
AwkwardCoder
  • 24,893
  • 27
  • 82
  • 152
mm8
  • 163,881
  • 10
  • 57
  • 88