3

我想知道是否有人遇到过这种情况。基本上我想要做的是覆盖默认的 listviewitem 来自定义选定的背景/前景。我得到了一切正常和花花公子。问题是,我注意到在我实现了网格视图的列表视图上,列被破坏了。我不确定发生了什么来破坏它。我覆盖默认样式的方法是使用 blend 通过编辑模板副本来获得完整样式。根据需要对其进行了修改。应用它。这几乎就是它的样子。有什么想法吗?

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

1 回答 1

5

我检查了ListViews这两种情况下的控件模板,并得出的结论是样式不起作用,GridViews因为它们需要 aGridViewRowPresenter来正确布局行数据,而不是ContentPresenter.

当然,如果你这样做,你会发现你的正常ListViews使用GridViews不再正确格式,因为它们需要一个ContentPresenter.

我不完全确定最好的解决方法,但偶然发现了这篇博文:http: //www.steelyeyedview.com/2010/03/contentpresenter-gridviewrowpresenter.html

我将在这里重复的要点,以防它被删除:

他的解决方案是一个巧妙的小技巧,而且似乎有效。它利用了两个演示者,ContentPresenter默认情况下是隐藏的 ( ),如果没有内容Visibility="Collapsed",它使用触发器使ContentPresenter可见。GridViewRowPresenter由于GridViewRowPresenter没有内容,因此无论如何它都不会显示任何内容。

调整你的Style内容以包含他的修复,你会有这样的东西(为了焦点而删除了一些代码):

<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>
于 2013-09-12T22:14:08.217 回答