C# 如何在WPF MVVM中更新UI时更改文本的颜色
我有以下XAMLC# 如何在WPF MVVM中更新UI时更改文本的颜色,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我有以下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"
<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>
这里的目的是根据内容更改进入状态列的文本的颜色。我希望当结果通过时有绿色文本,当结果失败时有红色文本
我已经读过,我应该创建一个实现DataTemplateSelector的类。但是,每当与这个XAML相关的面板更新时,我都无法在这个类中找到断点
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...
}
}
}
public class ResourceStaticAnalysisResults
{
public string Name { get; set; }
public List<string> ErrorList { get; set; }
public string Status { get; set; }
}
公共类ResourceStaticAnalysisResults
{
公共字符串名称{get;set;}
公共列表错误列表{get;set;}
公共字符串状态{get;set;}
}
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}" />
谢谢!我想数据模板应该放在右边?另外,我注意到您将数据类型定义为布尔值,但True和False标志是字符串。。应该用DataType=“{x:Type system:String}”替换吗?@user3587624DataType
应该设置为GridViewColumn
绑定到的任何类型,因为这是您提供模板的类型。在本例中,True和False是字符串(“True”和“False”),所以我将其设置为system:String。但是,在您的示例中,在刷新窗格之后,什么也不会发生。请不要因为在XAML中看到它们之间的引号而感到困惑——这总是适用于XML中的属性值。我知道,实际上我将值更改为Passed和Failed(而不是True或False)。它们绝对是弦。我将更新问题:)