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値が「Bad」のときに背景が赤くなるを作成したい。これは、ユーザー コントロールの 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. したがって、ここで起こっていることは、次の方法で viewModel からの値をバインドするcollapsed にバインドすることBackgroundで、表示の色を変更していることです。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