1
  1. 我有一个struct记录值和检查的类,它只是说明它是好还是坏。
    public enum StressCheck
    {
        Good,
        Bad
    }   

    public struct Stress
    {
        public Stress(double stressValue, StressCheck check)
        {
            StressValue = stressValue;
            Check = check;
        }
        public double StressValue { get; set; }               
        public StressCheck Check { get; set; }
    }
  1. 我想创建一个TextBlock当值为“坏”时其背景变为红色的。这就是我目前在我的用户控件的 XAML 中所做的。
<UserControl x:Class="ScienceProgram.UserControls.DataCellCheck"
             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" 
             x:Name="parent">
    <UserControl.Resources>
        <Style x:Key="CheckStress" TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=TB,Path=Text}" Value="Good">
                    <Setter Property="Background" Value="Green" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=TB,Path=Text}" Value="Bad">
                    <Setter Property="Background" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>
    <Grid Margin="1" DataContext="{Binding ElementName=parent}" Width="100">
        <TextBlock Style="{StaticResource CheckStress}" Text="{Binding Path=Value}" />
        <TextBlock x:Name="TB"  Text="{Binding Path=Check}" Visibility="Collapsed"/>
    </Grid>

以及依赖对象“Value”和“Check”的标准代码隐藏

    public partial class DataCellCheck : UserControl
    {
        public DataCellCheck()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty ValueProp = 
            DependencyProperty.Register("Value", typeof(string), 
                typeof(DataCellCheck), new PropertyMetadata(""));

        public string Value
        {
            get { return GetValue(ValueProp) as String; }
            set { SetValue(ValueProp, value); }
        }

        public static readonly DependencyProperty StatusProp = 
            DependencyProperty.Register("Check", typeof(string), 
                typeof(DataCellCheck), new PropertyMetadata(""));

        public string Check
        {
            get { return GetValue(StatusProp) as String; }
            set { SetValue(StatusProp, value); }
        }
    }
  1. 所以,这里发生的事情是我通过将它绑定到一个 collapsed 来改变Background我显示的颜色,在其中我以以下方式绑定来自 viewModel 的值。TextBlockTexblockStressCheck
    public class TestViewModel : BindableBase
    {
        public Stress MyFirstStress { get; set; }

        public TestViewModel()
        {
            MyFirstStress = new Stress(1245, StressCheck.Fail);
        }

        public double DisplayStressValue => MyFirstStress.StressValue;
        public string DisplayStressCheck => MyFirstStress.Check.ToString();
    }
        

在 XAML 中

<UserControl x:Class="ScienceProgram.Views.TestView"
             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:local="clr-namespace:ScienceProgram.Views"
             xmlns:uc="clr-namespace:ScienceProgram.UserControls"
             mc:Ignorable="d" 
             d:DesignHeight="1200" d:DesignWidth="811">
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="65"/>
        </Grid.RowDefinitions>
        <StackPanel>
            <uc:DataCellCheck Value="{Binding Path=DisplayStressValue }" Check="{Binding Path=DisplayStressCheck}"/>
        </StackPanel>
    </Grid>
</UserControl>
  1. 现在,我的问题是有没有更好的方法来触发显示的文本块中的颜色变化,而不必绑定StressCheck并将其隐藏在另一个文本块中?

非常感谢。

4

1 回答 1

0

有没有更好的方法来触发显示的文本块中的颜色变化,而不必绑定 StressCheck 并将其隐藏在另一个文本块中?

我应该希望如此。你现在这样做的方式非常笨拙。

如果没有最小、完整、可验证的代码示例,就不可能确认这在您的确切场景中是否有效,但它应该。您可以只绑定到Check触发器中的属性而不是其他属性TextBlock

<Style x:Key="CheckStress" TargetType="TextBlock">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Check}" Value="Good">
            <Setter Property="Background" Value="Green" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Check}" Value="Bad">
            <Setter Property="Background" Value="Red" />
        </DataTrigger>
    </Style.Triggers>
</Style>

对于它的价值,我更喜欢为一个值包含一个设置器,并为另一个(或其他,如果有多个)使用触发器:

<Style x:Key="CheckStress" TargetType="TextBlock">
    <Setter Property="Background" Value="Green"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding Check}" Value="Bad">
            <Setter Property="Background" Value="Red" />
        </DataTrigger>
    </Style.Triggers>
</Style>

无论哪种方式,WPF 都会enum隐式处理类型,将提供的文本值解析为实际的枚举值,并将其用于触发器的比较。您不需要额外的TextBlock或其他机制(例如转换器)来在您放入 XAML 的文本和实际enum值之间进行转换。

如果以上内容不能解决您的问题,请通过提供良好的 MCVE 来改进问题,以便可以理解您的场景的完整上下文。

于 2020-07-10T16:01:06.663 回答