Home > Blockchain >  C# WPF DataTrigger
C# WPF DataTrigger

Time:11-08

I want to make status indicators for each filler.When filler is stop mode it will change color to red, but when filler is running then green. For each line we have two fillers so i was trying to set up with datatrigger but didn't now how to program trigger/s for two borders because they using same property.

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>

Code for borders

                            <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>

CodePudding user response:

I think this is what you're trying to achieve.

<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="{Binging 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>

The code behind will need to implement INotifyPropertyChanged to update the UI when the value of LineStatus and LineStatus1 changes.

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;

    }

}
  • Related