【发布时间】: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