Untitled

 avatar
unknown
plain_text
2 years ago
1.7 kB
5
Indexable
<Window x:Class="YourNamespace.YourWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:YourNamespace"
        mc:Ignorable="d"
        Title="MVVM Example" Height="300" Width="300">

    <Window.DataContext>
        <local:MyViewModel/>
    </Window.DataContext>

    <Grid>
        <WrapPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <Border Background="{Binding BackgroundColor}" BorderThickness="1" BorderBrush="Black" Padding="5">
                <TextBlock Text="{Binding Text}"/>
                <Border.Style>
                    <Style TargetType="Border">
                        <!-- Default style if no DataTrigger matches -->
                        <Setter Property="Background" Value="Transparent"/>
                        <Style.Triggers>
                            <!-- DataTrigger to change the background color -->
                            <DataTrigger Binding="{Binding YourState}" Value="State1">
                                <Setter Property="Background" Value="LightGreen"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding YourState}" Value="State2">
                                <Setter Property="Background" Value="LightBlue"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Border.Style>
            </Border>
        </WrapPanel>
    </Grid>
</Window>
Editor is loading...