C# WPF 数据触发器

C# WPF DataTrigger

我想为每个 filler.When 填充器制作状态指示器,在停止模式下它将颜色变为红色,但当填充器为 运行 时则变为绿色。 对于每一行,我们有两个填充符,所以我试图用数据触发器设置,但现在不知道如何为两个边界编程 trigger/s,因为它们使用相同的 属性.

Fillers are in stop mode Filler 1 is running.

        <DataTrigger Binding="{Binding LineStatus}" Value="True">
        <Setter Property="BorderBrush" Value="Red"/>
    </DataTrigger>
    <DataTrigger Binding="{Binding LineStatus2}" Value="True">
        <Setter Property="BorderBrush" Value="Red"/>
    </DataTrigger>

边框代码

                            <StackPanel Orientation="Horizontal" >

                            <Border
                                 BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="4"                                       
                                    CornerRadius="5,0,0,5"
                                    Width="40"
                                    />
                                <Border
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="4"                                       
                                    CornerRadius="0,5,5,0"                                            
                                    Width="40"

                                    />
                            </StackPanel>

我认为这就是您要实现的目标。

<StackPanel Orientation="Horizontal">

    <Border BorderThickness="4"
            CornerRadius="5 0 0 5"
            Width="40">
        <Border.Style>
            <Style TargetType="Border">

                <!-- "Put your default color here." -->
                <Setter Property="BorderBrush" Value="Orange" />

                <Style.Triggers>
                    <DataTrigger Binding="{Binding LineStatus,
                                            UpdateSourceTrigger=PropertyChanged,
                                            FallbackValue='False'}"
                                            Value="True">

                        <!-- "Color to change to when LineStatus = True ." -->
                        <Setter Property="BorderBrush" Value="Red" />

                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Border.Style>
    </Border>
    <Border BorderThickness="4"
            CornerRadius="5 0 0 5"
            Width="40">
        <Border.Style>
            <Style TargetType="Border">

                <!-- "Put your default color here" -->
                <Setter Property="BorderBrush" Value="Orange" />

                <Style.Triggers>
                    <DataTrigger Binding="{Binging LineStatus1,
                                            UpdateSourceTrigger=PropertyChanged,
                                            FallbackValue='False'}"
                                            Value="True">

                        <!-- "Color to change to when LineStatus1 = True ." -->
                        <Setter Property="BorderBrush" Value="Red" />

                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Border.Style>
    </Border>

</StackPanel>

当 LineStatus 和 LineStatus1 的值发生变化时,后面的代码将需要实现 INotifyPropertyChanged 来更新 UI。

public partial class MainWindow : Window, INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged([CallerMemberName] string propertyname = null)
    {

        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));

    }

    private bool _lineStatus = false;
    public bool LineStatus
    {

        get { return _lineStatus; }
        set
        {

            _lineStatus = value;
            OnPropertyChanged();

        }

    }

    private bool _lineStatus1 = false;
    public bool LineStatus1
    {

        get { return _lineStatus1; }
        set
        {

            _lineStatus1 = value;
            OnPropertyChanged();
        }
    }

    public MainWindow()
    {

        InitializeComponent();

        LineStatus = false;
        LineStatus1 = true;

    }
}