【发布时间】:2012-12-20 01:04:21
【问题描述】:
我在UserControl.Resources 中设置了两种样式
<Style TargetType="{x:Type TextBlock}"> <Setter Property="Foreground" Value="white" /> </Style> <Style TargetType="{x:Type Label}"> <Setter Property="Foreground" Value="white" /> </Style>
因此,在我的DataTemplate 中(请注意,我已将其余部分切掉)我将应用白色文本,而无需更改每个 Label 和 TextBlock 元素的属性。
<DataTemplate x:Key="FileTransferItemTemplate">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Label Content="Transferring With: " />
<TextBlock Text="{Binding Path=OtherUserName, Mode=TwoWay}" />
</StackPanel>
</DataTemplate>
但是发生了什么(这给我带来了一个漫长的噩梦,我认为我的数据绑定不正确,因为我看不到任何更改),当数据被绑定时,前景色默认为黑色。我的数据绑定文本在黑色背景上是黑色的,我什至没有意识到最长的时间。
我可以覆盖它的唯一方法是在TextBlock 上手动设置Foreground="White"。 Label 适用于颜色,因为它不是数据绑定的。
为什么会发生这种情况,我该如何解决?
【问题讨论】:
-
如果你用一些文本替换绑定,你会意识到问题与绑定没有任何关系。
-
TextBlock 的默认样式似乎在 DataTemplate 中被忽略。找到this question,虽然它是 Silverlight,而不是 WPF。另请参阅this page。