DataTrigger on Enums as the trigger value on a WPF Style

房东的猫 提交于 2019-12-22 03:24:50

问题


So here's what I'm trying to do in a little nutshell, I'm just gonna start with code and it will most likely make sense.

<bl:InnerGlowBorder x:Name="glow"
                    InnerGlowColor="Teal">
  <bl:InnerGlowBorder.Style>
    <Style TargetType="bl:InnerGlowBorder">
      <Style.Triggers>
        <DataTrigger Binding="{Binding ViewUnitStatus}"
                     Value="UnitStatusModel.Pass">
          <Setter Property="InnerGlowColor"
                  Value="Green" />
        </DataTrigger>
        <DataTrigger Binding="{Binding ViewUnitStatus}"
                     Value="UnitStatusModel.Fail">
          <Setter Property="InnerGlowColor"
                  Value="Red" />
        </DataTrigger>
        <DataTrigger Binding="{Binding ViewUnitStatus}"
                     Value="UnitStatusModel.Indeterminate">
          <Setter Property="InnerGlowColor"
                  Value="Yellow" />
        </DataTrigger>
        <DataTrigger Binding="{Binding ViewUnitStatus}"
                     Value="UnitStatusModel.Warning">
          <Setter Property="InnerGlowColor"
                  Value="Orange" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </bl:InnerGlowBorder.Style>
</bl:InnerGlowBorder>

And the enum definition:

namespace SEL.MfgTestDev.ESS.ViewModel
{
    public enum UnitStatusModel
    {
        Indeterminate,
        Pass,
        Fail,
        Warning,
    }
}

Am I missing a piece to make this work? I've found some WPF articles on enums that rely on object data sources and I don't really like that solution, isn't there something more simple I can do here?


回答1:


I have found the solution and it was quite silly.

Styles are designed as a sort of visual template for a control, but they are designed as a base for visual implementation, not as a be-all/end-all visual model.

As a result, I had a situation in which my template dictated what the InnerGlowColor should be. However, by applying the attribute InnerGlowColor="Teal" to the element, I've created an override in effect, ignoring my visual style. The solution was to simply remove the dependancy property in the element declaration.



来源:https://stackoverflow.com/questions/1488547/datatrigger-on-enums-as-the-trigger-value-on-a-wpf-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!