【问题标题】:WPF - Move ListView.GroupStyle to external XAMLWPF - 将 ListView.GroupStyle 移动到外部 XAML
【发布时间】:2014-01-08 18:00:10
【问题描述】:

几个小时后我放弃了。 我有以下 ListView(在网格中),其中定义了 GroupStyle。 我想以某种方式将其取出并放入模板或样式中(我很困惑),然后将其添加到我的 ListView 的主要样式(ListViewSimpleStyle)中。 这样它就可以在其他地方重复使用,而不是每次都复制粘贴。

我该怎么做?

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

谢谢

【问题讨论】:

  • 请查看使用附加行为的question 的答案。

标签: wpf listview groupstyle


【解决方案1】:

Groupstyle 是 Groupbox 的样式,所以我们需要编辑 Groupbox 的样式,我已经更改了 GroupBox HeaderTemplate,因为您想更改 GroupStyle 的 HeaderTemplate。

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

并在您自己的列表视图模板样式中添加具有其样式的 GroupBox,即 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    相关资源
    最近更新 更多