首页 > 解决方案 > 将枚举与 DataTriggers 一起使用

问题描述

我有一个看起来像这样的枚举:

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

我有一个UserControl看起来像:

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

我的 DataTriggers 不会改变颜色。

我试图将导入语句从:

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

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

它没有编译。

 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); }
      }
 }

标签: wpfxaml

解决方案


推荐阅读