locked
Binding custom control property to object trigger RRS feed

  • Question

  • So basically I have a custom WPF control I have made with a property LineType the code for which is here (if needed):

     

    public enum LineType
    {
        Normal,
        Read,
        Important
    }
     
    // (the above is in a seperate file)
    // -------------------------------------
     
    private LineType t = LineType.Normal;
     
    public LineType LineType { get { return t; } set { t = value; } }
    

     

    What I am trying to do is have the control have a different background colour depending on the LineType property, even when the controls LineType is changed at run time I would like it to change the background too.

    So basically I need to bind the controls property to a Trigger, but it's not working.

    This is the code I have at the moment:

     

    <UserControl x:Class="WMail.Line"
                 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" 
                 mc:Ignorable="d" 
                 d:DesignHeight="30" d:DesignWidth="520" MinHeight="30" MinWidth="520" Loaded="UserControl_Loaded">
        <Border x:Name="mb">
            <Grid>
     
            </Grid>
            <Border.Style>
                <Style TargetType="Border">
                    <Setter Property="Background" Value="Red" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding LineType, ElementName=Line}" Value="Normal">
                            <Setter Property="Background" Value="Blue" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding LineType, ElementName=Line}" Value="Read">
                            <Setter Property="Background" Value="Green" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
        </Border>
    </UserControl>
    

    That just stays red though, any help is appreciated thank you :)

    EDIT

    Been playing around, got this now:

    <UserControl x:Class="WMail.Line"
                 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"
                 mc:Ignorable="d" 
                 d:DesignHeight="30" d:DesignWidth="520" MinHeight="30" MinWidth="520" Loaded="UserControl_Loaded">
        <UserControl.Resources>
            <Style x:Key="bg" TargetType="{x:Type Border}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=LineType, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" Value="Normal">
                        <Setter Property="Background" Value="Yellow" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=LineType, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" Value="Read">
                        <Setter Property="Background" Value="Purple" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=LineType, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" Value="Important">
                        <Setter Property="Background" Value="Green" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </UserControl.Resources>
        <Border x:Name="MainB" Style="{DynamicResource bg}">
            <Grid>
                
            </Grid>
        </Border>
    </UserControl>


    It works fine, but if I change the LineType at run time the colour doesn't change. Any way to fix this?

     


    • Edited by Banjo348 Sunday, November 27, 2011 4:02 AM
    Sunday, November 27, 2011 3:13 AM

Answers

All replies