WPF のラップ パネルに 2 つのボタンがあり、そのうちの 1 つにIsDefault="True"
. もう一方をクリックすると、「デフォルトボタン」になりたいです。
プレス イベントを聞いてプログラムでこれを実行できることはわかっていますが、あちこちに多くのコードを接続する必要のない普遍的なソリューションが必要です。
より宣言的なセッションで実装するEventTrigger
場合は、Button の Clicked イベントに対して WPF を定義し、IsDefault
影響を受けるボタンのプロパティを変更する Storyboard を定義できます。
これが私が何を意味するかを示すサンプルです:
<Window x:Class="TriggerSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Button x:Name="one" Content="One">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<EventTrigger RoutedEvent="Click">
<BeginStoryboard>
<Storyboard >
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(Button.IsEnabled)">
<DiscreteBooleanKeyFrame Value="False" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<Button Content="Two" />
</StackPanel>
</Window>
IsEnabled プロパティを変更して、機能することをテストできるようにします。次に、それを IsDefault プロパティに変更できます (明らかに値 True を取得します)。
最初のボタンのプロパティを作成して2番目のボタンの逆にInvertBooleanConverter
バインドすると、最初のボタンのトリガーを使用してオフに設定するだけで、2番目のボタンが自動的にオンに切り替わります。IsDefault
IsDefault
<Button x:Name="B1" IsDefault="True"/>
<Button x:Name="B2" IsDefault="{Binding ElementName=B1, IsDefault, Converter={StaticResource InvertBooleanConverter}}"/>
私のバインディングで彼の回答から「JanKratochvil」スタイルトリガーを使用する場合、これは完全XAMLで簡単に機能するはずです。