简体   繁体   English

WPF-将ListView.GroupStyle移至外部XAML

[英]WPF - Move ListView.GroupStyle to external XAML

After several hours I gave up. 几个小时后,我放弃了。 I have the following ListView (in Grid) with GroupStyle defined inside of it. 我有下面的ListView(在网格中),其中定义了GroupStyle。 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). 我想以某种方式将其取出,并放入模板或样式中(我很困惑),然后将其添加到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 谢谢

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. Groupstyle是Groupbox的样式,因此我们需要编辑Groupbox的样式,并且您想更改GroupStyle的HeaderTemplate时更改了GroupBox HeaderTemplate。

Visit this: http://msdn.microsoft.com/en-us/library/ms754027(v=vs.90).aspx 访问此网址http : //msdn.microsoft.com/zh-cn/library/ms754027(v=vs.90).aspx

and Add GroupBox With Its style in your own listview template style ie ListViewSimpleStyle 并在您自己的listview模板样式(即ListViewSimpleStyle)中添加具有其样式的GroupBox

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM