代码之家  ›  专栏  ›  技术社区  ›  Erez

WPF-列表视图选择样式

  •  1
  • Erez  · 技术社区  · 15 年前

    我正在使用一个包含两列的Listview控件,一列是文本,另一列是矩形init。 当选择一个项目时,我希望该项目的内容为白色(列1中的文本和列2中的矩形),但发生的情况是,只有文本变白。

    这是我的XAML:

    <Window x:Class="Selection.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" >
    <Grid>
        <Grid.Resources>
            <ResourceDictionary>
    
                <Style x:Key="@ListViewItemStyle" TargetType="{x:Type ListViewItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType='{x:Type ListViewItem}'>
                                <Grid SnapsToDevicePixels="True" Margin="0">
                                    <Border x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}" />
                                    <GridViewRowPresenter x:Name="Content" TextBlock.Foreground="{TemplateBinding Foreground}"
                                Content="{TemplateBinding Content}" Columns="{TemplateBinding GridView.ColumnCollection}" />
                                </Grid>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="true">
                                        <Setter Property="TextElement.Foreground" Value="White" TargetName="Content" />
                                        <Setter Property="Background" Value="DarkGray" TargetName="Bd"/>
                                    </Trigger>
                                    <MultiTrigger>
                                        <MultiTrigger.Conditions>
                                            <Condition Property="IsSelected" Value="true" />
                                            <Condition Property="Selector.IsSelectionActive" Value="false" />
                                        </MultiTrigger.Conditions>
                                        <Setter Property="Background" TargetName="Bd"
                                    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>
    
                <DataTemplate x:Key="@TextCellTemplate">
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
    
                <DataTemplate x:Key="@TrubleCellTemplate">
                    <Rectangle Width="20" Height="20" Fill="Black"></Rectangle>
                </DataTemplate>
    
            </ResourceDictionary>
        </Grid.Resources>
    
    
        <ListView ItemsSource="{Binding Persons}" Style="{DynamicResource @ListView}" ItemContainerStyle="{DynamicResource @ListViewItemStyle}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="40" CellTemplate="{DynamicResource @TextCellTemplate}" />
                    <GridViewColumn Width="131" CellTemplate="{DynamicResource @TrubleCellTemplate}" />
                </GridView>
            </ListView.View>
    
        </ListView>
    
    </Grid>
    

    这就是背后的密码:

      public partial class MainWindow : Window
    {
        public List<Person> Persons { get; set; }
    
        public MainWindow()
        {
            Persons = new List<Person> {new Person {Name = "Ashton"}};
            DataContext = this;
            InitializeComponent();
        }
    }
    
    public class Person
    {
        public string Name { get; set; }
    }
    

    alt text

    1 回复  |  直到 14 年前
        1
  •  2
  •   Fredrik Hedblad    15 年前

    您可以将IsSelected的数据触发器添加到@TrubleCellTemplate中,如下所示

    <DataTemplate x:Key="@TrubleCellTemplate">
        <Rectangle x:Name="rectangle" Width="20" Height="20" Fill="Black"></Rectangle>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}}"
                         Value="True">
                <Setter TargetName="rectangle" Property="Fill" Value="White"/>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
    
    推荐文章