ListViewItem的覆盖默认样式破GridView的 [英] ListViewItem override default style break gridviews

查看:148
本文介绍了ListViewItem的覆盖默认样式破GridView的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人曾经碰到这种情况我不知道。基本上我想要做的是覆盖默认的ListViewItem到定制选定的背景/前景。我得到了所有的工作很好,很正常。问题是,我注意到,在哪里我已实施的列表视图GridView的列被打破。我不知道这是怎么回事打破这一点。我来覆盖默认样式的方法被用于混合通过编辑模板的副本才能得到完整的风格。根据需要修改它。应用它。这是pretty多是什么样子。有什么想法?

 <风格的TargetType ={X:类型的ListViewItem}>
    < setter属性=背景值=透明/>
    < setter属性=Horizo​​ntalContentAlignmentVALUE ={结合Horizo​​ntalContentAlignment,的RelativeSource = {的RelativeSource AncestorType = {X:类型的ItemsControl}}}/>
    < setter属性=VerticalContentAlignmentVALUE ={结合VerticalContentAlignment,的RelativeSource = {的RelativeSource AncestorType = {X:类型的ItemsControl}}}/>
    < setter属性=填充VALUE =2,0,0,0/>
    < setter属性=模板>
    < Setter.Value>
    <的ControlTemplate的TargetType ={X:类型的ListViewItem}>
        < BORDER X:NAME =BD
            背景={TemplateBinding背景}
            BorderBrush ={TemplateBinding BorderBrush}
            了borderThickness ={TemplateBinding了borderThickness}
            填充={TemplateBinding填充}
            SnapsToDevicePixels =真正的>
            <内容presenter的Horizo​​ntalAlignment ={TemplateBinding Horizo​​ntalContentAlignment}
                VerticalAlignment ={TemplateBinding VerticalContentAlignment}
                SnapsToDevicePixels ={TemplateBinding SnapsToDevicePixels}/>
        < /边框>
    < ControlTemplate.Triggers>
        <触发属性=IsSelectedVALUE =真正的>
            <二传手的TargetName =BD属性=BorderBrushVALUE ={DynamicResource CustomBorderBrush}/>
            <二传手的TargetName =BD属性=背景VALUE ={DynamicResource CustomBackgroundBrush}/>
            < setter属性=前景VALUE ={DynamicResource CustomForegroundBrush}/>
        < /触发>
    < MultiTrigger>
        < MultiTrigger.Conditions>
            <条件属性=IsSelectedVALUE =真/>
            <条件属性=Selector.IsSelectionActiveVALUE =FALSE/>
    < /MultiTrigger.Conditions>
    <二传手的TargetName =BD属性=背景VALUE ={DynamicResource {X:静态SystemColors.ControlBrushKey}}/>
    < setter属性=前景VALUE ={DynamicResource {X:静态SystemColors.ControlTextBrushKey}}/>
    < / MultiTrigger>
    <触发属性=IsEnabledVALUE =false的>
        < setter属性=前景VALUE ={DynamicResource {X:静态SystemColors.GrayTextBrushKey}}/>
    < /触发>
    < /ControlTemplate.Triggers>
    < /控件模板>
    < /Setter.Value>
    < /二传手>
< /样式和GT;


 < ListView控件Grid.Row =0Grid.Column =0利润=15,15,0,0NAME =lstResources的SelectionChanged =lstResources_SelectionChanged >
        < ListView.View>
            <&GridView的GT;
                < GridView.Columns>
                    < GridViewColumn X:NAME =列1标题=列1WIDTH =100CellTemplate ={StaticResource的column1template}/>
                    < GridViewColumn X:NAME =列2标题=列2WIDTH =100CellTemplate ={StaticResource的column2template}/>
                    < GridViewColumn X:NAME =栏3标题=栏3WIDTH =200CellTemplate ={StaticResource的column3template}WPFUtility:GridViewColumnResize.Width =*/>
                < /GridView.Columns>
            < / GridView的>
        < /ListView.View>
< /&的ListView GT;


 <的DataTemplate X:键=column1template>
        <&DockPanel中GT;
            < TextBlock中的Horizo​​ntalAlignment =拉伸TextTrimming =CharacterEllipsis>
            < TextBlock.Text>
                <绑定路径=为myContent/>
            < /TextBlock.Text>
            < / TextBlock的>
        < / DockPanel中>
    < / DataTemplate中>


解决方案

我在两种情况下检查控制模板的列表视图,并得出结论,造型没有为 GridView的工作,因为它们需要的 GridViewRow presenter 正确布局行数据,而不是内容presenter

当然,如果你这样做,你会发现你的正常列表视图不使用 GridView的不再是正确的格式,因为它们需要一个内容presenter

我不能完全肯定的周围最巧妙的方法,而是跨越这篇博客文章跌跌撞撞:的http://www.steelyeyedview.com/2010/03/content$p$psenter-gridviewrow$p$psenter.html

其中,我会在这里重复,以防要点它被删除:

他的解决方案是一个整洁的小黑客,并似乎工作。它利用两个presenters,用内容presenter 默认是隐藏的(可见=折叠),并使用一个触发器,使内容presenter 可见,如果 GridViewRow presenter 没有内容。因为 GridViewRow presenter 没有内容,也不会显示任何东西反正。

使您的风格,包括他的修复,你有这样的事情(有些code焦点删除):

 <风格的TargetType ={X:类型的ListViewItem}>
    <! - 你的code - >
    < setter属性=模板>
        < Setter.Value>
            <的ControlTemplate的TargetType ={X:类型的ListViewItem}>
                < BORDER X:NAME =BD
                     背景={TemplateBinding背景}
                     BorderBrush ={TemplateBinding BorderBrush}
                     了borderThickness ={TemplateBinding了borderThickness}
                     填充={TemplateBinding填充}
                     SnapsToDevicePixels =真正的>
                    <网格和GT;
                        < GridViewRow presenter X:NAME =gridrow presenter
                            CONTENT ={TemplateBinding属性= ContentControl.Content}/>
                        <内容presenter X:NAME =内容presenter
                            CONTENT ={TemplateBinding属性= ContentControl.Content}能见度=坍塌/>
                    < /网格和GT;
                < /边框>
                < ControlTemplate.Triggers>
                    <触发属性=GridView.ColumnCollectionVALUE ={X:空}>
                        <二传手的TargetName =内容presenter属性=能见度VALUE =可见/>
                    < /触发>
                    <! - 你的code - >
                < /ControlTemplate.Triggers>
            < /控件模板>
        < /Setter.Value>
    < /二传手>
< /样式和GT;

I was wondering if anyone has ever run into this situation. Basically what I'm trying to do is override the default listviewitem to customized the the selected background/foreground. I got that working all fine and dandy. Problem is, I noticed that on listviews where I have implemented gridviews the columns are broken. I'm not sure what's going on to break this. My approach to override the default style is used blend to get the full style by editing a copy of template. Modified it as needed. Applied it. This is pretty much what it looks like. Any thoughts?

<Style TargetType="{x:Type ListViewItem}">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
    <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
    <Setter Property="Padding" Value="2,0,0,0" />
    <Setter Property="Template">
    <Setter.Value>
    <ControlTemplate TargetType="{x:Type ListViewItem}">
        <Border x:Name="Bd"
            Background="{TemplateBinding Background}"
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}"
            Padding="{TemplateBinding Padding}"
            SnapsToDevicePixels="true">
            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
        </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsSelected" Value="true">
            <Setter TargetName="Bd" Property="BorderBrush" Value="{DynamicResource CustomBorderBrush}" />
            <Setter TargetName="Bd" Property="Background" Value="{DynamicResource CustomBackgroundBrush}" />
            <Setter Property="Foreground" Value="{DynamicResource CustomForegroundBrush}" />
        </Trigger>
    <MultiTrigger>
        <MultiTrigger.Conditions>
            <Condition Property="IsSelected" Value="true" />
            <Condition Property="Selector.IsSelectionActive" Value="false" />
    </MultiTrigger.Conditions>
    <Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
    </MultiTrigger>
    <Trigger Property="IsEnabled" Value="false">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
    </Trigger>
    </ControlTemplate.Triggers>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
</Style>


<ListView Grid.Row="0" Grid.Column="0" Margin="15,15,0,0" Name="lstResources" SelectionChanged="lstResources_SelectionChanged">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn x:Name="column1"  Header="column1" Width="100" CellTemplate="{StaticResource column1template}"/>
                    <GridViewColumn x:Name="column2" Header="column2" Width="100" CellTemplate="{StaticResource column2template}" />
                    <GridViewColumn x:Name="column3" Header="column3" Width="200" CellTemplate="{StaticResource column3template}" WPFUtility:GridViewColumnResize.Width="*"/>
                </GridView.Columns>
            </GridView>
        </ListView.View>
</ListView>


<DataTemplate x:Key="column1template">
        <DockPanel>
            <TextBlock HorizontalAlignment="stretch" TextTrimming="CharacterEllipsis" >
            <TextBlock.Text>
                <Binding Path="mycontent"/>
            </TextBlock.Text>
            </TextBlock>
        </DockPanel>
    </DataTemplate>

解决方案

I inspected the control templates for the ListViews in both cases, and came to the conclusion that the styling wasn't working for GridViews because they require a GridViewRowPresenter to correctly layout the row data, rather than the ContentPresenter.

Of course, if you do that, you'll find your normal ListViews which don't use GridViews no longer format correctly, because they require a ContentPresenter.

I wasn't entirely sure of the neatest way around that, but stumbled across this blog post: http://www.steelyeyedview.com/2010/03/contentpresenter-gridviewrowpresenter.html

The gist of which I'll repeat here, in case it gets deleted:

His solution is a neat little hack, and seems to work. It makes use of both presenters, with the ContentPresenter hidden by default (Visibility="Collapsed"), and uses a trigger to make the ContentPresenter visible if the GridViewRowPresenter has no content. Since the GridViewRowPresenter has no content, it won't show anything anyway.

Adapting your Style to include his fix, you'd have something like this (Some code removed for focus):

<Style TargetType="{x:Type ListViewItem}">
    <!-- Your Code -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Border x:Name="Bd"
                     Background="{TemplateBinding Background}"
                     BorderBrush="{TemplateBinding BorderBrush}"
                     BorderThickness="{TemplateBinding BorderThickness}"
                     Padding="{TemplateBinding Padding}"
                     SnapsToDevicePixels="true">
                    <Grid>
                        <GridViewRowPresenter x:Name="gridrowPresenter"
                            Content="{TemplateBinding Property=ContentControl.Content}" />
                        <ContentPresenter x:Name="contentPresenter"
                            Content="{TemplateBinding Property=ContentControl.Content}"  Visibility="Collapsed" />
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="GridView.ColumnCollection" Value="{x:Null}">
                        <Setter TargetName="contentPresenter" Property="Visibility" Value="Visible"/>
                    </Trigger>
                    <!-- Your Code -->
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这篇关于ListViewItem的覆盖默认样式破GridView的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆