是否可以在GridView模式下隐式设置WPF ListView的样式?

是否可以在GridView模式下隐式设置WPF ListView的样式?,第1张

概述我可以在基本(非GridView)模式下轻松地“隐式”设置ListView样式,但是我在GridView模式下隐式设置ListView样式的尝试却失败了.下面的工作原理是因为我明确设置了第二个ListView的Style和ItemContainerStyle.如果删除这两个设置,则第二个ListView不会像第一个那样进行隐式设置.似乎基本的ListView需要ContentPresenter,而 我可以在基本(非GrIDVIEw)模式下轻松地“隐式”设置ListVIEw样式,但是我在GrIDVIEw模式下隐式设置ListVIEw样式的尝试却失败了.下面的工作原理是因为我明确设置了第二个ListVIEw的Style和ItemContainerStyle.如果删除这两个设置,则第二个ListVIEw不会像第一个那样进行隐式设置.似乎基本的ListVIEw需要ContentPresenter,而GrIDVIEw ListVIEw需要GrIDVIEwRowPresenter.

我在这里跑到WPF砖墙吗?这甚至可能吗?如果没有,它会使创建应用程序外观不那么健壮,因为现在您的用户必须知道在GrIDVIEw模式下显示的ListVIEw上显式设置Style和ItemContainerStyle.

<Window x:Class="WpfApplication1.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:system="clr-namespace:System;assembly=mscorlib"        title="MainWindow" Height="350" WIDth="525">    <Window.Resources>        <Style targettype="ListVIEw">            <Setter Property="Background" Value="lime"/>        </Style>        <Style targettype="ListVIEwItem">            <Setter Property="Background" Value="Yellow"/>            <Setter Property="Template">              <Setter.Value>                <ControlTemplate targettype="{x:Type ListBoxItem}">                    <GrID>                      <ContentPresenter x:name="ContentHost" margin="{TemplateBinding padding}"                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />                    </GrID>                </ControlTemplate>              </Setter.Value>            </Setter>        </Style>        <Style x:Key="{x:Static GrIDVIEw.GrIDVIEwStyleKey}"               targettype="{x:Type ListVIEw}">            <Setter Property="Background" Value="lime"/>        </Style>        <Style x:Key="{x:Static GrIDVIEw.GrIDVIEwItemContainerStyleKey}"               targettype="{x:Type ListVIEwItem}">            <Setter Property="Background" Value="Yellow"/>        </Style>    </Window.Resources>    <GrID>        <GrID.ColumnDeFinitions>            <ColumnDeFinition WIDth="*"/>            <ColumnDeFinition WIDth="*"/>        </GrID.ColumnDeFinitions>        <ListVIEw x:name="_ListVIEw1">            <system:String>Item 1</system:String>            <system:String>Item 2</system:String>            <system:String>Item 3</system:String>        </ListVIEw>        <ListVIEw x:name="_ListVIEw2" GrID.Column="1"                   Style="{StaticResource {x:Static GrIDVIEw.GrIDVIEwStyleKey}}"                  ItemContainerStyle="{StaticResource {x:Static GrIDVIEw.GrIDVIEwItemContainerStyleKey}}">            <ListVIEw.VIEw>                <GrIDVIEw>                    <GrIDVIEwColumn header="Date"/>                    <GrIDVIEwColumn header="Day of Week" displayMemberBinding="{Binding DayOfWeek}" />                    <GrIDVIEwColumn header="Year" displayMemberBinding="{Binding Year}" />                </GrIDVIEw>            </ListVIEw.VIEw>            <system:DateTime>1/1/2010</system:DateTime>            <system:DateTime>1/1/2011</system:DateTime>            <system:DateTime>1/1/2012</system:DateTime>        </ListVIEw>    </GrID></Window>
解决方法 我遇到了与你上面描述的相同的奇怪/棘手的问题,然后遇到了一个博客文章,其中提出了一个简洁的小黑客/修复程序,它似乎提供了你所追求的行为.在链接死亡的情况下重复要点.

您已经描述了ListVIEw样式的奇怪要求,如果您想要使用GrIDVIEws来覆盖视图并使用GrIDVIEws;一个基本的ListVIEw需要一个ContentPresenter,一个GrIDVIEw ListVIEw需要一个GrIDVIEwRowPresenter.

海报设法通过在他的风格中包括两个演示者,并使用Setter仅在需要时显示ContentPresenter来解决这个问题.

因此,您的ControlTemplate可以沿着这些方向实现某些功能(根据需要添加额外的样式属性):

<ControlTemplate targettype="{x:Type ListBoxItem}">    <!-- Pair of presenters -->    <GrID>        <GrIDVIEwRowPresenter x:name="grIDrowPresenter"                Content="{TemplateBinding Property=ContentControl.Content}"/>        <ContentPresenter x:name="contentPresenter"                Content="{TemplateBinding Property=ContentControl.Content}"  Visibility="Collapsed"/>    </GrID>    <!-- Visibility Controlling Setter -->    <ControlTemplate.Triggers>        <Trigger Property="GrIDVIEw.ColumnCollection" Value="{x:Null}">            <Setter Targetname="contentPresenter" Property="Visibility" Value="Visible"/>        </Trigger>    </ControlTemplate.Triggers></ControlTemplate>

GrIDVIEwRowPresenter和ContentPresenter都存在于样式中,但ContentPresenter是隐藏的(Visibility =“Collapsed”).

巧妙的技巧是在GrIDVIEw.ColumnCollection上使用Trigger;如果此值为null(当GrIDVIEwRowPresenter没有内容时发生),则ContentPresenter将变为可见,正确显示正常的ListVIEw内容). GrIDVIEwRowPresenter没有内容,因此不会显示任何冲突的视觉效果.

如果GrIDVIEw具有内容,则将显示(提供正确的行格式),并且ContentPresenter将保持隐藏状态.

原创博客文章:http://www.steelyeyedview.com/2010/03/contentpresenter-gridviewrowpresenter.html

总结

以上是内存溢出为你收集整理的是否可以在GridView模式下隐式设置WPF ListView的样式?全部内容,希望文章能够帮你解决是否可以在GridView模式下隐式设置WPF ListView的样式?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1128816.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存