gpt4 book ai didi

c# - 如何在用户控件中以样式定义触发器

转载 作者:太空狗 更新时间:2023-10-29 23:18:04 25 4
gpt4 key购买 nike

我有一个用户控件,其中我有几个文本框/日历/...控件,它们都应该根据我的用户控件 (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>

最佳答案

您需要设置 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); }
}

关于c# - 如何在用户控件中以样式定义触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6571349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com