【发布时间】:2012-11-28 16:25:01
【问题描述】:
xml代码:
<ControlTemplate x:Key="ChkTemplate"
TargetType="ListViewItem">
<StackPanel Orientation="Horizontal">
<CheckBox Margin="0,0,3,0">
<CheckBox.IsChecked>
<Binding Path="IsSelected"
Mode="TwoWay">
<Binding.RelativeSource>
<RelativeSource Mode="TemplatedParent" />
</Binding.RelativeSource>
</Binding>
</CheckBox.IsChecked>
</CheckBox>
<ContentPresenter />
</StackPanel>
</ControlTemplate>
<DataTemplate DataType="{x:Type ABC:Info}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}"
Margin="0,0,10,5" Foreground="Green"/>
<TextBlock Text="{Binding Channel}"
Margin="3,0,0,0"
Visibility="{Binding Path=Visible,ElementName=View, Converter={StaticResource BooleanConverter}}" />
<TextBlock.Foreground>
<SolidColorBrush Color="{Binding Foreground}" />
</TextBlock.Foreground>
</StackPanel>
</DataTemplate>
<Style TargetType="ListViewItem"
x:Key="SelectedItem">
<Setter Property="Template"
Value="{StaticResource ChkTemplate}" />
</Style>
类:
public class Info : DependencyObject
{
public Brush Foreground
{
get { return (Brush)GetValue(ForegroundProperty); }
set { SetValue(ForegroundProperty, value); }
}
}
xaml.cs:
private readonly RangeObservableCollection<Info> _validInfo;
Info.Foreground = Brushes.Red;
_validInfo.Add(Info);
上面的代码没有改变文本块的前景色我做错了什么?
【问题讨论】:
-
您不需要 SolidColorBrush 吗?我不认为画笔有颜色属性。
-
第二个TextBlock的DataContext是什么?这个对象真的有一个叫做 Foreground 的属性吗?您可以使用snoop 轻松检查。您还应该检查调试输出是否有任何警告,失败的绑定通常会打印在输出中。
-
@ColinPear - 你认为使用 SolidColorBrush 会起作用吗?我没有收到任何错误?
-
@dowhilefor - 没有收到任何编译错误或警告。
-
@ColinPear - 尝试使用 SolidColorBrush 没有任何区别颜色仍然是绿色。