gpt4 book ai didi

wpf - 如何设置 WPF ListView 选定项颜色?

转载 作者:行者123 更新时间:2023-12-03 23:22:03 25 4
gpt4 key购买 nike

我正在尝试在 Windows 7 上运行的 WPF 应用程序中从 Windows 8 重新创建邮件 UI。这是我想要实现的目标:

Target UI

特别是,我不知道如何更改所选项目的背景颜色,例如第一列中的收件箱项目和第二列中来自 Twitter 的邮件。我已经尝试了其他类似 Stackoverflow Questions 的几种解决方案,但似乎没有一个对我有用。例如

Selected item loses style when focus moved out in WPF ListBox

WPF ListView Inactive Selection Color

这是我的 ListView 的代码:

<ListView Grid.Row="0" SelectedItem="{Binding Path=SelectedArea}" ItemsSource="{Binding Path=Areas}" Background="#DCE3E5" >

<ListView.Resources>

<!-- Template that is used upon selection of an Area -->
<ControlTemplate x:Key="SelectedTemplate" TargetType="ListViewItem">
<Border Background="#388095" Cursor="Hand" >
<TextBlock Text="{Binding Name}" Margin="5" />
</Border>
</ControlTemplate>

<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<!-- Base Template that is replaced upon selection -->
<ControlTemplate TargetType="ListViewItem">
<Border Background="#DCE3E5" Cursor="Hand" >
<TextBlock Text="{Binding Name}" Margin="5" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true" />
</MultiTrigger.Conditions>
<Setter Property="Template" Value="{StaticResource SelectedTemplate}" />
</MultiTrigger>
</Style.Triggers>
</Style>

</ListView.Resources>

</ListView>

如何更改所选项目的背景颜色?以及如何在焦点变化时保留颜色变化。

最佳答案

我最近做了类似的事情:

<ListView.Resources>                
<ControlTemplate x:Key="SelectedTemplate" TargetType="ListViewItem">
<Border CornerRadius="5" BorderThickness="1" BorderBrush="DarkGray" Background="#FF92C6F9" Padding="2" HorizontalAlignment="Left" Margin="5" Tag="{Binding Value}" Cursor="Hand" MouseUp="Border_MouseUp_1">
<TextBlock Text="{Binding Name}" Margin="5" />
</Border>
</ControlTemplate>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border CornerRadius="5" BorderThickness="1" BorderBrush="DarkGray" Background="WhiteSmoke" Padding="2" HorizontalAlignment="Left" Margin="5" Tag="{Binding Value}" Cursor="Hand" MouseUp="Border_MouseUp_1" >
<TextBlock Text="{Binding Name}" Margin="5" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true" />
<Condition Property="Selector.IsSelectionActive" Value="true" />
</MultiTrigger.Conditions>
<Setter Property="Template" Value="{StaticResource SelectedTemplate}" />
</MultiTrigger>
</Style.Triggers>
</Style>
</ListView.Resources>

我相信删除:
<Condition Property="Selector.IsSelectionActive" Value="true" />

将允许您在失去焦点后保持背景颜色。

编辑:

在下面回答您的问题:

可以将 TextBlock 的 tag 属性绑定(bind)到 command 参数,然后在 TextBlock 的 MouseUp 事件上执行命令:
<TextBlock x:Name="MyTextBlock" Text="Click Me!" Tag="{Binding MyCommandParameter}" MouseUp="MyTextBlock_MouseUp" />

在后面的代码中:
    private void MyTextBlock_MouseUp(object sender, MouseButtonEventArgs e)
{
TextBlock tb = sender as TextBlock;

if (tb != null && tb.Tag != null)
{
ViewModel.MyCommand.Execute(tb.Tag);
}
}

关于wpf - 如何设置 WPF ListView 选定项颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14838524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com