【问题标题】:how to define triggers in style in user control如何在用户控件中以样式定义触发器
【发布时间】:2011-09-28 02:51:28
【问题描述】:

我有一个用户控件,其中有几个文本框/日历/...控件,它们都应该根据我的用户控件 (IsEditing) 中的属性启用或禁用

我在我的用户控件中创建了一个依赖属性 IsEditing,现在我想定义一个样式,所有控件都使用它来启用或禁用。

我正在尝试做这样的事情:

<ad:DocumentContent.Resources>
    <Style x:Key="ReadOnlyMode">
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="VerticalAlignment" Value="Stretch"/>
        <Setter Property="Visibility" Value="Visible"/>
        <Setter Property="IsEnabled" Value="False" />
        <Style.Triggers>
            <Trigger Property="IsEditing" Value="True">
                <Setter Property="Background" Value="Green"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ad:DocumentContent.Resources>

但我收到此错误:在“FrameworkElement”类型中找不到属性“IsEditing”。

如何处理触发器的正确​​属性?


更新 1:

我将 IsEditing 定义如下:

public static readonly DependencyProperty IsEditingProperty = DependencyProperty.Register(
            "IsEditing", typeof(Boolean), typeof(MyDetailDataControl), new PropertyMetadata(false));

并定义样式如下:

<ad:DocumentContent.Resources>
    <Style x:Key="ReadOnlyMode" xmlns:u="clr-namespace:MyProject.Controls" TargetType="{x:Type u:MyDetailDataControl}">
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="VerticalAlignment" Value="Stretch"/>
        <Setter Property="Visibility" Value="Visible"/>
        <Setter Property="IsEnabled" Value="False" />
        <Style.Triggers>
            <Trigger Property="IsEditing" Value="True">
                <Setter Property="Background" Value="Green"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ad:DocumentContent.Resources>

但我现在收到以下错误: 在类型“MyDetailDataControl”中找不到属性“IsEditing”。

我认为依赖属性定义有问题。


更新 2:

我将 xaml 更改如下(在用户控制部分添加 xmlns:l="clr-namespace:MyProject.Controls" 以便命名空间随处可用)并使用此处定义的方法http://www.wpfmentor.com/2009/01/how-to-debug-triggers-using-trigger.html 进行调试触发器)

<ad:DocumentContent.Resources>
    <Style x:Key="ReadOnlyMode" TargetType="TextBox">
        <Style.Triggers>
            <Trigger my:TriggerTracing.TriggerName="BoldWhenMouseIsOver" my:TriggerTracing.TraceEnabled="True" Property="l:MyDetailDataControl.IsEditing" Value="True">
                 <Setter Property="TextBox.Background" Value="Green"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ad:DocumentContent.Resources>

但是触发器没有被触发?我需要实现通知属性机制吗?如果是,我该怎么做?当属性是附加属性时,是否有任何特殊技术可以实现属性更改通知?


更新 3: 改成这个版本,和我自己的 IsEditing 属性无关。但是还是不行。

<ad:DocumentContent.Resources>
    <Style x:Key="MyStyle" TargetType="{x:Type l:MyDetailDataControl}" >
        <Style.Triggers>
            <Trigger my:TriggerTracing.TriggerName="BoldWhenMouseIsOver" my:TriggerTracing.TraceEnabled="True" Property="IsMouseOver" Value="True">
                <Setter Property="FontWeight" Value="Bold"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ad:DocumentContent.Resources>

【问题讨论】:

    标签: c# wpf xaml triggers styles


    【解决方案1】:

    您需要将样式的TargetType 设置为您的UserControl 的类型。或者你用它作为所有属性的前缀。

    <!-- Using TargetType -->
    <Style xmlns:u="clr-namespace:Test.UserControls"
           x:Key="ReadOnlyMode"
           TargetType="{x:Type u:MyUserControl1}">
        <Style.Triggers>
            <Trigger Property="IsEditing" Value="True">
                <!-- ... -->
            </Trigger>
        </Style.Triggers>
    </Style>
    <!-- Using qualified property -->
    <Style xmlns:u="clr-namespace:Test.UserControls"
           x:Key="ReadOnlyMode">
        <Style.Triggers>
            <Trigger Property="u:MyUserControl1.IsEditing" Value="True">
                <!-- ... -->
            </Trigger>
        </Style.Triggers>
    </Style>
    

    关于您的其他问题:该属性需要一个 CLR-Wrapper:

    public static readonly DependencyProperty IsEditingProperty =
            DependencyProperty.Register("IsEditing", typeof(Boolean), typeof(MyDetailDataControl), new UIPropertyMetadata(false));
    // This:
    public Boolean IsEditing
    {
        get { return (Boolean)GetValue(IsEditingProperty); }
        set { SetValue(IsEditingProperty, value); }
    }
    

    【讨论】:

    • 如果我使用 arget 类型作为 TextBox,它不能解决问题,并且我收到此错误:在类型 'TextBox' 中找不到属性 'IsEditing'。
    • 查看我的编辑,我说的是你的 UserControl 的类型,而不是它的基类。
    • 有两种样式和两种触发器。我应该两个都用还是一个就够了?
    • @user,选择其中之一。它们都做同样的事情,只是语法不同。
    • @user:这是您可以使用的两种不同方法。如果您处理多个自定义属性,我建议使用第一种方法。 (实际上我会建议任何一种方式)
    猜你喜欢
    • 2012-03-26
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多