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

时间:2013-04-16 20:03:24

标签: c# .net wpf .net-4.0 datatrigger

我继续围着这个数据触发器转圈,所以它不起作用......

我有一个带有默认阴影边框的按钮。但是,我想创建一个dep属性来用于切换它。然而,我从来没有达到设定效果的程度。

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

这是基于一个按钮,但实现为自定义用户控件...这是遗留代码...

1 个答案:

答案 0 :(得分:1)

我在这里有什么用 这是在新的WPF窗口中完成的。没有其他代码隐藏,而不是你在这里看到的。

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

代码隐藏:

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