[英]Updating Custom Attached Property in Style Trigger with Setter
I was trying out attached properties and style triggers hoping to learn more about it. 我正在尝试附加属性和样式触发器,希望能够更多地了解它。 I wrote a very simple WPF windows app with an attached property:
我写了一个非常简单的WPF Windows应用程序,附带一个属性:
public static readonly DependencyProperty SomethingProperty =
DependencyProperty.RegisterAttached(
"Something",
typeof(int),
typeof(Window1),
new UIPropertyMetadata(0));
public int GetSomethingProperty(DependencyObject d)
{
return (int)d.GetValue(SomethingProperty);
}
public void SetSomethingProperty(DependencyObject d, int value)
{
d.SetValue(SomethingProperty, value);
}
And I was trying to update the 'Something' attached property with a property trigger defined in the button style section: 我试图用按钮样式部分中定义的属性触发器更新'Something'附加属性:
<Window x:Class="TestStyleTrigger.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestStyleTrigger;assembly=TestStyleTrigger"
Title="Window1" Height="210" Width="190">
<Window.Resources>
<Style x:Key="buttonStyle" TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="local:Window1.Something" Value="1" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Button Style="{StaticResource buttonStyle}"></Button>
</Window>
However, I kept getting following compilation error: 但是,我不断收到编译错误:
error MC4003: Cannot resolve the Style Property 'Something'. 错误MC4003:无法解析样式属性'Something'。 Verify that the owning type is the Style's TargetType, or use Class.Property syntax to specify the Property.
验证拥有类型是Style的TargetType,还是使用Class.Property语法指定Property。 Line 10 Position 29.
第10行第29位。
I can't understand why it gives me this error because I did use the 'Class.Property' syntax in the tag of the section. 我无法理解为什么它会给我这个错误,因为我在该部分的标签中使用了'Class.Property'语法。 Can any one tell me how can I fix this compilation error?
任何人都可以告诉我如何解决这个编译错误?
Your backing methods for the dependency property are named incorrectly and must be static: 您对依赖项属性的支持方法命名不正确,必须是静态的:
public static int GetSomething(DependencyObject d)
{
return (int)d.GetValue(SomethingProperty);
}
public static void SetSomething(DependencyObject d, int value)
{
d.SetValue(SomethingProperty, value);
}
Also, you shouldn't specify the assembly in the local XML NS mapping in the XAML because the namespace is in the current assembly. 此外,您不应在XAML中的本地XML NS映射中指定程序集,因为名称空间位于当前程序集中。 Do this instead:
改为:
xmlns:local="clr-namespace:TestStyleTrigger"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.