Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am working on some WPF template creating the grouping checkbox. On first level checkbox is not required. Second level child is having checkbox and group of childrens also have checkbox.
Ex:
RootNode
 -> []Child                 x.x.x.x    first   none
   -> [] sub child 1        y.y.y.y    second  -
   -> [] sub child 2
 -> []Child 2
   -> [] sub child2.1       z.z.z.z    third   needed
RootNode2
 -> []Child
   -> [] sub child          a.a.a.a    value   not required


Could any one help on this?

What I have tried:

HTML
<ListView Grid.Row="2" Grid.RowSpan="2" Name="lvRecommendations" ItemsSource="{Binding Path=ListOfRootNode}" Height="350" ScrollViewer.HorizontalScrollBarVisibility="Auto">
                
                <ListView.Resources>
                    <Style TargetType="{x:Type GridViewColumnHeader}">
                        <Setter Property="HorizontalContentAlignment" Value="Left" />
                    </Style>
                    <Style x:Key="FirstLevel" TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander IsExpanded="True" HorizontalAlignment="Stretch" Grid.Column="0" Grid.ColumnSpan="2">
                                    <Expander.Header>
                                        <Border HorizontalAlignment="Stretch" Padding="2" Grid.ColumnSpan="2" >
                                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch"  >
                                                <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Grid.RowSpan="2" HorizontalAlignment="Stretch"></TextBlock>
                                            </StackPanel>
                                        </Border>
                                    </Expander.Header>
                                        <Grid Background="#FFFFFF">
                                            <ItemsPresenter />
                                        </Grid>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>

                    <Style x:Key="SecondLevel" TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate x:Name="ctemp" TargetType="{x:Type GroupItem}">
                                    <Expander x:Name="ComponentExpander" IsExpanded="True" HorizontalAlignment="Stretch" Grid.Column="0" Grid.ColumnSpan="2">
                                        <Expander.Header>
                                            <Border HorizontalAlignment="Stretch" Padding="2" Grid.ColumnSpan="2" >
                                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch"  >                                                    
                                                    <CheckBox IsThreeState="True">
                                                        <CheckBox.IsChecked>
                                                            <MultiBinding Converter="{StaticResource CheckboxConverter}"
                                                                Mode="OneWay">
                                                                    <MultiBinding.Bindings>
                                                                    <Binding Path="DataContext.ListOfRootNode"
                                                                            RelativeSource="{RelativeSource AncestorType={x:Type ListView}}"
                                                                         Mode="OneWay"/>
                                                                    <Binding Path="Name" Mode="OneWay" />
                                                                </MultiBinding.Bindings>
                                                            </MultiBinding>
                                                        </CheckBox.IsChecked>
                                                        <i:Interaction.Triggers>
                                                            <i:EventTrigger EventName="Checked" >
                                                                <i:InvokeCommandAction Command="{Binding DataContext.SelectComponentTypeCommand,
                                                                RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"
                                                                CommandParameter="{Binding Name}"/>
                                                                    </i:EventTrigger>
                                                            <i:EventTrigger EventName="Unchecked" >
                                                                <i:InvokeCommandAction Command="{Binding DataContext.DeSelectComponentTypeCommand,
                                                                RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"
                                                                CommandParameter="{Binding Name}"/>
                                                            </i:EventTrigger>
                                                        </i:Interaction.Triggers>
                                                    </CheckBox>
                                                    <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Grid.RowSpan="2" HorizontalAlignment="Stretch" />
                                                    <TextBlock Text="{Binding ItemCount}" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/>
                                                    <TextBlock Text=" Setting(s)" FontStyle="Italic" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" Tag="2"/>
                                                </StackPanel>
                                            </Border>
                                        </Expander.Header>
                                        <Grid Background="#FFFFFF">
                                            <ItemsPresenter />
                                        </Grid>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>

                    <local:GroupItemStyleSelector x:Key="GroupItemSelectorObj" FirstLevel="{StaticResource FirstLevel}" SecondLevel="{StaticResource SecondLevel}"/>

                </ListView.Resources>
                
                <ListView.View>
                    <GridView>
                        <GridViewColumn>
                            <GridViewColumn.HeaderTemplate>
                                <DataTemplate>
                                    <TextBlock Text="Setting" />
                                </DataTemplate>
                            </GridViewColumn.HeaderTemplate>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal" x:Name="settingPanel">
                                        <CheckBox Name="chkBoxComponent" IsChecked="{Binding IsChecked}" Checked="OnItemChecked" Unchecked="OnItemUnChecked" />
                                        <TextBlock Text="{Binding SettingName}" />
                                    </StackPanel>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn>
                            <GridViewColumn.HeaderTemplate>
                                <DataTemplate>
                                    <TextBlock Text="Image Name"/>
                                </DataTemplate>
                            </GridViewColumn.HeaderTemplate>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding ImageName}"/>
                                    </StackPanel>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn>
                            <GridViewColumn.HeaderTemplate>
                                <DataTemplate>
                                    <TextBlock Text="Priority"/>
                                </DataTemplate>
                            </GridViewColumn.HeaderTemplate>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding PriorityName}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand"/>
                                        <StackPanel Orientation="Horizontal" Margin="3,0,3,0" >
                                            <TextBlock Text="["  />
                                            <TextBlock Text="Notes" TextDecorations="Underline" Foreground="Blue" Cursor="Hand"/>
                                            <TextBlock Text="]"  />
                                        </StackPanel>
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text="["  />
                                            <TextBlock Text="CVA" TextDecorations="Underline" Foreground="Blue" Cursor="Hand"/>
                                            <TextBlock Text="]"  />
                                        </StackPanel>
                                    </StackPanel>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn>
                            <GridViewColumn.HeaderTemplate>
                                <DataTemplate>
                                    <TextBlock Text="Comments"/>
                                </DataTemplate>
                            </GridViewColumn.HeaderTemplate>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding Comments}" />
                                    </StackPanel>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
                <ListView.GroupStyle>
                    <GroupStyle ContainerStyleSelector="{StaticResource GroupItemSelectorObj}" />
                </ListView.GroupStyle>

            </ListView>
Posted
Updated 11-Jan-19 2:36am
v3
Comments
Graeme_Grant 11-Jan-19 6:18am    
where are you stuck?
Andrew Baylis 12-Jan-19 19:24pm    
Have you thought of using a TreeView with a Hierarchal template? This would allow you to have one template for the top level and a different one (including your check box) for the children.
sdileep1 17-Jan-19 23:35pm    
I have created the template like this but this is not working when the second template group checked childrens are not selected and vice versa. I was stuck at threestate is not working for checkbox? could anyone one help on this?

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