WPF:当字符串的值为 null 或 string.Empty 时触发数据触发器

发布于 2024-11-03 04:42:19 字数 1576 浏览 1 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事还在继续 2024-11-10 04:42:20

尝试在您的 Person 类上实现更改通知。这可能是你的问题的原因。

Try implementing Change Notification on your Person class. That might be the cause of your problem.

喜爱纠缠 2024-11-10 04:42:20

绑定正确,但您看不到任何内容,因为您的 Border 为 0×0 像素。你可以像这样得到想要的效果:

<Border Width="50">
    <TextBlock Text="{Binding Image}" />
    <!-- your style unchanged -->
</Border>

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:

<Border Width="50">
    <TextBlock Text="{Binding Image}" />
    <!-- your style unchanged -->
</Border>
桃扇骨 2024-11-10 04:42:20

正如 AbdouMoumen 所建议的,在您的 Person 类上实现 INotifyPropertyChanged。

public class Person : INotifyPropertyChanged
{
    private string _image;
    public string Image
    {
        get { return _image; }
        set
        {
            _image = value;
            NotifyPropertyChanged("Image");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

那么只有设置 person.Image = null 才会对您的 xaml 产生影响。

As AbdouMoumen has suggested, implement INotifyPropertyChanged on your Person class.

public class Person : INotifyPropertyChanged
{
    private string _image;
    public string Image
    {
        get { return _image; }
        set
        {
            _image = value;
            NotifyPropertyChanged("Image");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

Then only setting person.Image = null will have effect on your xaml.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文