簡體   English   中英

在WPF MVVM中更新UI時如何更改文本的顏色

[英]How to change color of text when UI gets updated in WPF 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"
             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得到更新,我就無法在此類中遇到斷點。

  1. 我的XAML邏輯出了什么問題?
  2. 我可能在ViewModel邏輯上遺漏了其他東西嗎?
  3. 我需要在課堂上實現哪種代碼才能更改文本的顏色?

謝謝!

更新:結果屬性與以下類關聯

public class ResourceStaticAnalysisResults
    {
        public string Name { get; set; }
        public List<string> ErrorList { get; set; }
        public string Status { 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}" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM