基于 DataContext 属性的触发器

发布于 2024-10-05 00:02:53 字数 1316 浏览 1 评论 0原文

假设我想根据 DataContext 中的属性值显示/隐藏元素,如何实现?

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

在 XAML 中,我想根据 Mode 显示隐藏元素。我怎样才能使下面的工作?或者实现这个的适当方法是什么?

<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>

目前,我收到的错误是“在类型“StackPanel”中找不到属性“模式”。D:\tmp\WpfApplication1\TriggersAndProperties\MainWindow.xaml

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; } 

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>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

怼怹恏 2024-10-12 00:02:53

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

<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 属性。

请注意,Dean Chalk 的答案仍然有效:如果您希望触发 Mode 的更改,则必须使用 DependencyProperty 或实现 INotifyPropertyChanged

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>

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.

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.

黯淡〆 2024-10-12 00:02:53

您的属性“模式”需要是依赖属性才能以这种方式使用:

public class MainViewModel : DependencyObject
{
    readonly DependencyProperty ModeProperty = DependencyProperty
        .Register("Mode", typeof(int), typeof(MainViewModel));

    public int Mode
    {
        get { return (int) GetValue(ModeProperty); }
        set { SetValue(ModeProperty, value); }
    }
}

Your propert 'Mode' needs to be a dependency property in order to be used this way:

public class MainViewModel : DependencyObject
{
    readonly DependencyProperty ModeProperty = DependencyProperty
        .Register("Mode", typeof(int), typeof(MainViewModel));

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