简体   繁体   English

如何从父属性设置触发器以设置子属性

[英]How to set Trigger from parent property to set child property

I keep going around in circles on this data trigger so it is not working... 我一直在这个数据触发器上转圈,所以它不起作用...

I have a button that has a border for a default dropshadow. 我有一个带有边框的默认下拉阴影按钮。 However, I want to create a dep property to be used to toggle this. 但是,我想创建一个dep属性来切换它。 Yet, I never get to the point where the effect is being set. 但是,我从来没有达到要设定效果的地步。

<Style x:Key="RoundedButton" TargetType="{x:Type Button}">
<Setter Property="Template">
 <Setter.Value>
  <ControlTemplate TargetType="ctrls:RoundedButton">
   <Grid>
    <Border>
     <Border.Style>
      <Style TargetType="ctrls:RoundedButton">
       <Style.Triggers>
        <Trigger Property="IsDropShadowVisible" Value="True">
         <Setter Property="Effect">
          <Setter.Value>
           <DropShadowEffect ShadowDepth="1"/>
          </Setter.Value>
         </Setter>
        </Trigger>
       </Style.Triggers>
      </Style>
     </Border.Style>

This is based off of a button, but is implemented as the custom user control...This is legacy code... 这是基于按钮的,但是实现为自定义用户控件。这是旧版代码。

What I have here works Did this in a new WPF window. 我在这里所做的工作是在新的WPF窗口中完成的。 No other code-behind than what you see here. 除了您在此处看到的代码外,没有其他代码。

<Window.Resources>
    <Style TargetType="{x:Type local:ShadowButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:ShadowButton}">
                    <Button Name="Button"></Button>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsDropShadowVisible" Value="True">
                            <Setter TargetName="Button" Property="Effect">
                                <Setter.Value>
                                    <DropShadowEffect ShadowDepth="1"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<!-- snip code -->

<local:ShadowButton Height="10" Width="10" IsDropShadowVisible="true"/>

Code-behind: 代码隐藏:

public class ShadowButton : Button
{
    public DependencyProperty IsDropShadowVisibleProperty =
        DependencyProperty.Register("IsDropShadowVisible", typeof(Boolean), typeof(ShadowButton), new PropertyMetadata(false));
    public Boolean IsDropShadowVisible
    {
        get { return (Boolean)GetValue(IsDropShadowVisibleProperty); }
        set { SetValue(IsDropShadowVisibleProperty, value); }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM