我正在尝试附加属性和样式触发器,希望了解更多信息。我编写了一个非常简单的带有附加属性的 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);
}
我正在尝试使用按钮样式部分中定义的属性触发器来更新“某物”附加属性:
<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>
但是,我不断收到以下编译错误:
错误 MC4003:无法解析样式属性“某事”。验证所属类型是否为 Style 的 TargetType,或使用 Class.Property 语法来指定 Property。第 10 行位置 29。
我不明白为什么它会给我这个错误,因为我确实在该部分的标签中使用了“Class.Property”语法。谁能告诉我如何解决这个编译错误?