WPF - Move ListView.GroupStyle to external XAML

705 views Asked by At

After several hours I gave up. I have the following ListView (in Grid) with GroupStyle defined inside of it. I want to take it out somehow and put it in a Template or Style (I'm confused) and then add it to my main Style of the ListView (ListViewSimpleStyle). This way it will be reusable in other places instead of copy-paste it every time.

How do I do it?

<ListView Name="LvDataBinding" Grid.Row="0"
                      Style="{StaticResource ListViewSimpleStyle}">                                             
                <!-- Define the grouping-->
                <ListView.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock FontSize="12" Text="{Binding Name}"
                                           Foreground="{StaticResource GrayForgroundBrush}"></TextBlock>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                    </GroupStyle>
                </ListView.GroupStyle>
            </ListView>

Thanks

1

There are 1 answers

0
Heena On

Groupstyle is style of Groupbox so we need to edit style of Groupbox and I have changed GroupBox HeaderTemplate as you want to change HeaderTemplate of GroupStyle.

Visit this:http://msdn.microsoft.com/en-us/library/ms754027(v=vs.90).aspx

and Add GroupBox With Its style in your own listview template style i.e ListViewSimpleStyle

     <Style x:Key="ListViewSimpleStyle" TargetType="ListView">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListView">
                    <Grid Background="AliceBlue" >


                        <GroupBox>
                            <GroupBox.Style>                                    
                                <Style TargetType="GroupBox">
                                    <Setter Property="HeaderTemplate">
                                        <Setter.Value>
                                            <DataTemplate>
                                                <TextBlock FontSize="12" Text="{Binding Name}" Foreground="{StaticResource GrayForgroundBrush}"/>
                                            </DataTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </GroupBox.Style>
                        </GroupBox>


                        <ItemsPresenter></ItemsPresenter>

                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>