WPF:当字符串的值为 null 或 string.Empty 时触发数据触发器
我创建了一个 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=""
仍然不起作用。
请帮忙,谢谢。
I have created a 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>
The Person
class looks like this:
public class Person
{
public string Image { get; set; }
}
The presented data trigger in the data template does not work as expected. No matter if I provide the value for the Image
property or not, it simply won't draw the linear gradient brush background. On the other hand, the text block is showing the content of the Image
property as it should.
I thought that maybe when the person.Image = null;
, then the XAML is in fact reading it as person.Image = string.Empty;
but I tried replacing Value="{x:Null}"
with Value=""
and it still doesn't work.
Help please, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试在您的 Person 类上实现更改通知。这可能是你的问题的原因。
Try implementing Change Notification on your Person class. That might be the cause of your problem.
绑定正确,但您看不到任何内容,因为您的
Border
为 0×0 像素。你可以像这样得到想要的效果:The binding is correct, but you don't see anything because your
Border
is 0×0 px. You can get the desired effect like this:正如 AbdouMoumen 所建议的,在您的 Person 类上实现 INotifyPropertyChanged。
那么只有设置 person.Image = null 才会对您的 xaml 产生影响。
As AbdouMoumen has suggested, implement INotifyPropertyChanged on your Person class.
Then only setting person.Image = null will have effect on your xaml.