【问题标题】:How to set Trigger from parent property to set child property如何从父属性设置触发器以设置子属性
【发布时间】:2013-04-09 09:19:51
【问题描述】:

我一直在这个数据触发器上转圈,所以它不起作用......

我有一个带有默认阴影边框的按钮。但是,我想创建一个 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>

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

【问题讨论】:

  • 发布完整的 XAML。另外,DataTrigger 没有意义。使用普通的Trigger Property=IsDropShadowVisible.. etc..
  • @HighCore 更新了我的答案
  • 您的 XAML 没有意义。你有一个Style TargetType="Button",然后是一个ControlTemplate TargetType="ctrls:RoundedButton"。我建议您查看 this tutorial 了解 XAML 的介绍性内容。
  • @HighCore yah....我刚刚添加了一条注释...我正在尝试解决这个问题,但它嵌入在其他地方并导致问题...有什么办法吗否则呢?还是我需要修复这些遗留问题
  • 旧版?我不认为这意味着你认为那意味着什么。

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


【解决方案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); }
    }
}

【讨论】:

  • 如果您考虑到这条评论,那么这解决了我的问题:只需删除边框样式,命名边框,然后给设置器目标名称。完成。
猜你喜欢
  • 1970-01-01
  • 2011-03-10
  • 2013-08-30
  • 2012-11-23
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多