我有以下XAML
<UserControl x:Class="DomainExperience.Pane.DomainFile.DomainAnalysisPaneResultsControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:domainExperience="clr-namespace:DomainExperience"
xmlns:domainFile="clr-namespace:DomainExperience.Pane.DomainFile"
Background="{DynamicResource VsBrush.Window}"
Foreground="{DynamicResource VsBrush.WindowText}"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Name="MyToolWindow">
<UserControl.Resources>
<domainFile:StatusColoredDataTemplateClass x:Key="StatusColoredDataTemplate" />
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<ListView Grid.Row="0" ItemsSource="{Binding Path=Results}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Status" DisplayMemberBinding="{Binding Status}" CellTemplateSelector="{DynamicResource StatusColoredDataTemplate}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</UserControl>
然后,我创建了以下类
using System.Windows;
using System.Windows.Controls;
namespace DomainExperience.Pane.DomainFile
{
public class StatusColoredDataTemplateClass : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
// Some logic here that I haven't defined yet...
}
}
}
此处的目的是根据内容更改进入“状态”列的文本的颜色。我希望结果为Passed时为绿色文本,为失败时为红色文本。
我已经读过我应该创建一个实现DataTemplateSelector的类。但是,只要与此XAML相关的Panel得到更新,我就无法在此类中遇到断点。
谢谢!
更新:结果属性与以下类
相关联public class ResourceStaticAnalysisResults
{
public string Name { get; set; }
public List<string> ErrorList { get; set; }
public string Status { get; set; }
}
答案 0 :(得分:3)
DataTemplateSelector
过度,因为您不需要更改DataTemplate
本身,只需更改其中一个控件的属性即可。相反,您可以为该列定义单个固定DataTemplate
并使用样式管理颜色更改:
<DataTemplate x:Key="StatusColoredDataTemplate" DataType="{x:Type system:Boolean}">
<TextBlock Text="{Binding}">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Green" />
<Style.Triggers>
<DataTrigger Binding="{Binding}" Value="False">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
专栏:
<GridViewColumn
Header="Status"
DisplayMemberBinding="{Binding Status}"
CellTemplate="{StaticResource StatusColoredDataTemplate}" />