【发布时间】:2011-08-11 14:13:43
【问题描述】:
我创建了一个DataTemplate
<DataTemplate DataType="{x:Type model:Person}">
<StackPanel>
<!-- Text box is binding to the person's Image property. -->
<TextBlock Text="{Binding Image}" />
<Border>
<Border.Style>
<Style TargetType="Border">
<Style.Triggers>
<!-- Data trigger is binding to the same Image property. -->
<DataTrigger Binding="{Binding Image}" Value="{x:Null}">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#696969" Offset="0.0" />
<GradientStop Color="#2E2E2E" Offset="1.0" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
</StackPanel>
</DataTemplate>
Person 类如下所示:
public class Person
{
public string Image { get; set; }
}
数据模板中显示的数据触发器未按预期工作。无论我是否提供Image 属性的值,它都不会绘制线性渐变画笔背景。另一方面,文本块正按应有的方式显示Image 属性的内容。
我认为也许当person.Image = null; 时,XAML 实际上将其读取为person.Image = string.Empty;,但我尝试将Value="{x:Null}" 替换为Value="",但它仍然不起作用。
请帮忙,谢谢。
【问题讨论】:
标签: wpf xaml triggers datatrigger