How to set Panel.ZIndex or other attached properties via DataTrigger?

↘锁芯ラ 提交于 2020-01-02 15:26:09

问题


I am trying to have a circular overlay come to the top when a certain view model enters an offline state. So it becomes partially transparent and on top of other elements in the Grid.

DataTriggers in the style have worked for everything so far, but I cannot set Panel.ZIndex. There is no error in build or run, but the property is not set (I assume because it's an attached property?)

<Ellipse Fill="DarkGray" Panel.ZIndex="-10" Width="50" Height="50">
  <Ellipse.Style TargetType="Ellipse">
    <Style>
      <Style.Triggers>
        <DataTrigger Binding="{Binding Status}" Value="Offline">
            <Setter Property="Opacity" Value=".6" />
            <Setter Property="Panel.ZIndex" Value="10" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Ellipse.Style>
</Ellipse>

回答1:


You have the syntax correct, however the problem is that you are defining Panel.ZIndex in the <Ellipse> tag, and properties set in the tag itself will take precedence over any triggered values.

To fix it, simply set Panel.ZIndex in your style instead of the Ellipse tag

<Ellipse Fill="DarkGray" Width="50" Height="50">
  <Ellipse.Style TargetType="Ellipse">
    <Style>
      <Setter Property="Panel.ZIndex" Value="-10" />
      <Style.Triggers>
        <DataTrigger Binding="{Binding Status}" Value="Offline">
            <Setter Property="Opacity" Value=".6" />
            <Setter Property="Panel.ZIndex" Value="10" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Ellipse.Style>
</Ellipse>

See MSDN's article on Dependency Property Precedence for more info



来源:https://stackoverflow.com/questions/9793001/how-to-set-panel-zindex-or-other-attached-properties-via-datatrigger

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