私はそれについてもっと学びたいと思って、添付されたプロパティとスタイル トリガーを試していました。添付プロパティを使用して、非常に単純な 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>
ただし、次のコンパイルエラーが発生し続けました。
error MC4003: スタイル プロパティ 'Something' を解決できません。所有する型がスタイルの TargetType であることを確認するか、Class.Property 構文を使用してプロパティを指定します。行 10 位置 29。
セクションのタグで「Class.Property」構文を使用したため、このエラーが発生する理由がわかりません。このコンパイルエラーを修正する方法を教えてもらえますか?