基于DataContext属性的触发器 [英] Triggers Based on Properties from DataContext

查看:56
本文介绍了基于DataContext属性的触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想基于DataContext中的Properties值显示/隐藏元素,如何实现呢?

Suppose I want to show/hide elements based on Values of Properties from DataContext, how can I acheive it?

// In MainWindow.xaml.cs: DataContext of MainWindow.xaml
public int Mode { get; set; } 

在XAML中,我想显示基于 Mode 。我如何使以下工作?还是实现此目的的适当方法是什么?

In XAML, I want to show hide elements based on the Mode. How can I make the below work? Or what is the appropriate way of implementing this?

<StackPanel>
    <StackPanel.Triggers>
        <Trigger Property="Mode" Value="1">
            <Setter TargetName="txt1" Property="Visibility" Value="Visible" />
            <Setter TargetName="txt2" Property="Visibility" Value="Collapsed" />
            <Setter TargetName="txt3" Property="Visibility" Value="Visible" />
        </Trigger>
        <Trigger Property="Mode" Value="2">
            <Setter TargetName="txt1" Property="Visibility" Value="Collapsed" />
            <Setter TargetName="txt2" Property="Visibility" Value="Visible" />
            <Setter TargetName="txt3" Property="Visibility" Value="Collapsed" />
        </Trigger>
    </StackPanel.Triggers>
    <TextBlock Text="TextBlock 1" x:Name="txt1" />
    <TextBlock Text="TextBlock 2" x:Name="txt2" />
    <TextBlock Text="TextBlock 3" x:Name="txt3" />
</StackPanel>

当前,我得到的错误是 在类型中找不到属性'Mode' D:Patmp\WpfApplication1\TriggersAndProperties\MainWindow.xaml

Currently, the Error I am getting is "Property 'Mode' was not found in type 'StackPanel'. D:\tmp\WpfApplication1\TriggersAndProperties\MainWindow.xaml"

推荐答案

如果要使用可以在绑定上使用的触发器,则需要使用 DataTriggers 。问题是, DataTriggers 仅在样式和模板上可用,因此您需要这样定义:

You need to use DataTriggers if you want triggers that can work on bindings. Problem is, DataTriggers are only available on style and template so you need to define one like this:

<StackPanel>
  <StackPanel.Style>
    <Style TargetType="{x:Type StackPanel}">
      <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Mode}" Value="1">
          <Setter TargetName="txt1" Property="Visibility" Value="Visible" />
          <Setter TargetName="txt2" Property="Visibility" Value="Collapsed" />
          <Setter TargetName="txt3" Property="Visibility" Value="Visible" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=Mode}" Value="2">
          <Setter TargetName="txt1" Property="Visibility" Value="Collapsed" />
          <Setter TargetName="txt2" Property="Visibility" Value="Visible" />
          <Setter TargetName="txt3" Property="Visibility" Value="Collapsed" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </StackPanel.Style>
  <TextBlock Text="TextBlock 1" x:Name="txt1" />
  <TextBlock Text="TextBlock 2" x:Name="txt2" />
  <TextBlock Text="TextBlock 3" x:Name="txt3" />
</StackPanel>

另一种解决方案是使用 IValueConverter 将int从 Mode 转换为所需的 Visibility ,并将其直接应用于每个文本块 Visibility 属性。

Another solution would be to use an IValueConverter that converts the int from Mode to the Visibility you want, and apply it directly to each text block Visibility property.

请注意,Dean Chalk的答案仍然有效:您必须使用 DependencyProperty 或实施 INotifyPropertyChanged 如果要触发 Mode 上的更改。

Note that Dean Chalk's answer stays valid: you have to use a DependencyProperty or implement INotifyPropertyChanged if you want changes on Mode to trigger.

这篇关于基于DataContext属性的触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆