The problem
I defined a reusable control, MyControl, that extends a TextBox.
我定义了一个可重用的控件MyControl,它扩展了TextBox。
I want to set a Trigger to one of its dependency properties.
So I added a style to it, with Triggers.
我想将Trigger设置为其依赖属性之一。所以我用Triggers为它添加了一个样式。
But if I set the TargetType of the Style to MyControl, I get a XAML warning that 'MyControl' TargetType does not match type of element 'TextBlock'
.
And if I set it to TextBlock, I get a compilation error that The member "MyDependencyProperty" is not recognized or is not accessible.
.
但是如果我将Style的TargetType设置为MyControl,我会收到一条XAML警告:'MyControl'TargetType与元素'TextBlock'的类型不匹配。如果我将它设置为TextBlock,我得到一个编译错误,成员“MyDependencyProperty”无法识别或无法访问..
- How can I define this style with the triggers?
- 如何使用触发器定义此样式?
Sample
C# code-behind
namespace UserControls.Local
{
public partial class MyControl : TextBlock
{
#region Trogdor
public static readonly DependencyProperty TrogdorProperty = DependencyProperty.Register(
"Trogdor", typeof (bool), typeof (MyControl), new PropertyMetadata(default(bool)));
public bool Trogdor
{
get { return (bool) GetValue(TrogdorProperty); }
set { SetValue(TrogdorProperty, value); }
}
#endregion
public MyControl()
{
InitializeComponent();
}
}
}
XAML
<TextBlock
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:UserControls.Local"
mc:Ignorable="d"
Text="BOOP!"
x:Class="UserControls.Local.MyControl">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Blue"/>
<Style.Triggers>
<Trigger Property="Trogdor" Value="True">
<Setter Property="Foreground" Value="DeepPink"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
2 个解决方案
#1
3
The solution I found was to "fully qualify" the dependency property on the binding:
我发现的解决方案是“完全限定”绑定的依赖属性:
<Trigger Property="local:MyControl.Trogdor" Value="True">
#2
1
Not sure if you are still looking for a solution, but the answer in this thread worked for me, while yours didn't.
不确定你是否仍在寻找解决方案,但这个问题的答案对我有用,而你的答案却没有。
It uses a DataTrigger with a binding on the root element, instead of a Trigger:
它使用DataTrigger与根元素绑定,而不是触发器:
<DataTrigger Binding="{Binding Path=Highlight, RelativeSource={RelativeSource AncestorType={x:Type Elements:DataElement}}}" Value="True">
<Setter Property="Control.Background" Value="{DynamicResource EntryBoxHighlightBackground}"/>
</DataTrigger>
With your solution, I press the button and the value of the variable changes but the style trigger doesn't apply the changes, like it was not informed of the change in the variable.
使用您的解决方案,我按下按钮,变量的值会发生变化,但样式触发器不会应用更改,就像没有通知变量的变化一样。
#1
3
The solution I found was to "fully qualify" the dependency property on the binding:
我发现的解决方案是“完全限定”绑定的依赖属性:
<Trigger Property="local:MyControl.Trogdor" Value="True">
#2
1
Not sure if you are still looking for a solution, but the answer in this thread worked for me, while yours didn't.
不确定你是否仍在寻找解决方案,但这个问题的答案对我有用,而你的答案却没有。
It uses a DataTrigger with a binding on the root element, instead of a Trigger:
它使用DataTrigger与根元素绑定,而不是触发器:
<DataTrigger Binding="{Binding Path=Highlight, RelativeSource={RelativeSource AncestorType={x:Type Elements:DataElement}}}" Value="True">
<Setter Property="Control.Background" Value="{DynamicResource EntryBoxHighlightBackground}"/>
</DataTrigger>
With your solution, I press the button and the value of the variable changes but the style trigger doesn't apply the changes, like it was not informed of the change in the variable.
使用您的解决方案,我按下按钮,变量的值会发生变化,但样式触发器不会应用更改,就像没有通知变量的变化一样。