WPFを覚えているだけで、この動作を把握することはできません。例:WPF:DataTriggerがデフォルトのスタイルを削除するのはなぜですか?
ラベルは、ウィンドウ上のStaticResourceからその初期のスタイルを取得します。
- ボタンを使用して(分離コード経由)
- チェックボックスを使用して(DataTriggerが結合経由):その後、私はラベルのスタイルを変更するには、2つの方法があります。
ボタンをクリックすると、CheckBoxでスタイルを変更できなくなりました。 申し訳ありませんが、なぜこれが起こるのか分かりません。 :-)ここで私を助けることができる誰にも事前に感謝コードです:
XAML:
<Window x:Class="WtfDataTrigger.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WtfDataTrigger"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="Label" x:Key="LabelStyle">
<Setter Property="Foreground" Value="Red" />
</Style>
</Window.Resources>
<StackPanel>
<Label Name="Label1" Content="Label1" FontSize="24">
<Label.Style>
<Style TargetType="Label" BasedOn="{StaticResource LabelStyle}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=Checkbox, Path=IsChecked}" Value="True">
<Setter Property="Foreground" Value="Aqua" />
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label >
<Button Content="Click Me to Change Label Style Using Routed Events" FontSize="18" Click="Button_Click" />
<CheckBox Name="Checkbox" Content="Check Me to Change Label Style Using Data Trigger" FontSize="18">
</CheckBox>
</StackPanel>
</Window>
分離コード:バインディングは、ボタンクリック時に失われているため
private void Button_Click(object sender, RoutedEventArgs e)
{
Label1.Foreground = new SolidColorBrush(Colors.DarkOrange);
}