我指的是代码块here,对数据触发为什么样式targettype必须重新绑定回原始属性?
<Window x:Class="WpfTutorialSamples.Styles.StyleDataTriggerSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="StyleDataTriggerSample" Height="200" Width="200">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<CheckBox Name="cbSample" Content="Hello, world?" />
<TextBlock HorizontalAlignment="Center" Margin="0,20,0,0" FontSize="48">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="No" />
<Setter Property="Foreground" Value="Red" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=cbSample, Path=IsChecked}" Value="True">
<Setter Property="Text" Value="Yes!" />
<Setter Property="Foreground" Value="Green" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
</Window>
对我来说,从编程语言设计点,线<Style TargetType="TextBlock">
是完全没有必要的,因为它已经被嵌套在<TextBlock>
里面,这样的当然,无论哪种设置属性都应该应用于TextBlock
类型。那么为什么需要行<Style TargetType="TextBlock">
?
可以<Style TargetType>
除TextBlock
之外的其他类型吗?
*你可以使用任何类TextBlock派生自* ---什么? – Graviton
'class CustomizedTextBlock:TextBlock'可以使用'Style'与'TargetType =“TextBlock”'(例如...)。而'TextBlock'本身则来自'FrameworkElement'。所以你可以为'TextBlock Style'使用'TargetType =“FrameworkElement”'。但我看不到为简单的“TextBlock”所做的努力...... – WPFGermany