0

I have an enum that looks like this:

namespace CryptoRecovery.Models.Experiment.Cells
{
     public enum ExperimentState
     {
          On,
          Off
     }
}

I have a UserControl that looks like :

    <UserControl x:Class="CryptoRecovery.Views.Experiments.OnOffSwitch"
             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:cells="clr-namespace:CryptoRecovery.Models.Experiment.Cells"
             mc:Ignorable="d" >

    <Border Height="30" Width="81" CornerRadius="15" BorderThickness="1">
        <Border.Style>
            <Style TargetType="Border">
                <Setter Property="BorderBrush" Value="{StaticResource OffColorBrush}"></Setter>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=State, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" Value="{x:Static cells:ExperimentState.On}">
                        <Setter Property="BorderBrush" Value="{StaticResource OnColorBrush}"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Border.Style>
        <TextBlock Text="{Binding Path=State}" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Setter Property="Foreground" Value="{StaticResource OffColorBrush}"></Setter>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=State, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" Value="On">
                            <Setter Property="Foreground" Value="{StaticResource OnColorBrush}"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </Border>
</UserControl>

My DataTriggers do not change the colors.

I have tried to change the import statement from:

xmlns:cells="clr-namespace:CryptoRecovery.Models.Experiment.Cells"

to

xmlns:cells="clr-namespace:CryptoRecovery.Models.Experiment.Cells;assembly=CryptoRecovery.Models"

and it did not compile.

 public partial class OnOffSwitch : UserControl
 {
      public OnOffSwitch()
      {
           InitializeComponent();
      }

      public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
           "State", typeof(ExperimentState), typeof(OnOffSwitch), new FrameworkPropertyMetadata(default(ExperimentState), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

      public ExperimentState State
      {
           get { return (ExperimentState) GetValue(StateProperty); }
           set { SetValue(StateProperty, value); }
      }
 }
3xGuy
  • 2,235
  • 2
  • 29
  • 51
  • The DataTrigger Setters don't work because the directly assigned property values for BorderBrush and Foreground have higher precedence. Move these assignments to Style Setters. – Clemens May 12 '20 at 13:41
  • @Clemens I have updated that it still doesn't update. State is a Dependency Property – 3xGuy May 12 '20 at 13:51
  • Is State a property of the UserControl? There is also `Text="{Binding Path=State}"` without RelativeSource. How is that supposed to work? – Clemens May 12 '20 at 14:14
  • yes, State is a `DependencyProperty `of the `UserControl` the Text does work. It by default does `.ToString()` and returns "On" or "Off" – 3xGuy May 12 '20 at 14:17
  • How, without RelativeSource? Do not set the DataContext of the UserControl to itself. Doing so will break any DataContext-based Bindings of its properties, like ``. – Clemens May 12 '20 at 14:23
  • That is actually what fixed this. – 3xGuy May 12 '20 at 14:39
  • Ok, please close the question as a duplicate, e.g. of this: https://stackoverflow.com/q/28981862/1136211 – Clemens May 12 '20 at 14:41

0 Answers0