【问题标题】:How can I change the Visibility of a TextBlock with a Trigger?如何使用触发器更改 TextBlock 的可见性?
【发布时间】:2010-10-29 05:12:54
【问题描述】:

当我尝试编译以下代码时,我收到错误 'Visibility' member is not valid because it does not have aqualified type name.

我必须进行哪些更改才能在 Status=off 时使 TextBlock 消失

XAML:

<Window x:Class="TestTrigger123345.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <TextBlock Text="This is a sentence.">
            <TextBlock.Triggers>
                <Trigger Property="{Binding Status}" Value="off">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </Trigger>
            </TextBlock.Triggers>
        </TextBlock>
        <TextBlock Text="{Binding Status}"/>
    </StackPanel>
</Window>

代码隐藏:

using System.Windows;

namespace TestTrigger123345
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            DataContext = this;
            Status = "off";
        }

        public string Status { get; set; }

    }
}

我更改为 DataTrigger 和 Dependency Properties 并得到相同的错误:

XAML:

<Window x:Class="TestTrigger123345.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel HorizontalAlignment="Left">
        <TextBlock Text="{Binding Status}">
            <TextBlock.Triggers>
                <DataTrigger Binding="{Binding Status}" Value="off">
                    <Setter Property="TextBlock.Background" Value="Tan"/>
                </DataTrigger>
            </TextBlock.Triggers>
        </TextBlock>
    </StackPanel>
</Window>

代码隐藏:

using System.Windows;

namespace TestTrigger123345
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            DataContext = this;
            Status = "off";
        }

        #region DependencyProperty: Status
        public string Status
        {
            get
            {
                return (string)GetValue(StatusProperty);
            }
            set
            {
                SetValue(StatusProperty, value);
            }
        }

        public static readonly DependencyProperty StatusProperty =
            DependencyProperty.Register("Status", typeof(string), typeof(Window1),
            new FrameworkPropertyMetadata());
        #endregion


    }
}

我使用具有实现 INotifyPropertyChanged 的​​属性 Status 的 ViewModel 重新执行此操作,但它得到了同样的错误:

WindowViewModel.cs:

using System.ComponentModel;

namespace TestTrigger123345
{
    class WindowViewModel
    {
        #region ViewModelProperty: Status
        private string _status;
        public string Status
        {
            get
            {
                return _status;
            }

            set
            {
                _status = value;
                OnPropertyChanged("Status");
            }
        }
        #endregion

        #region PropertChanged Block
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
        #endregion
    }
}

代码隐藏:

using System.Windows;

namespace TestTrigger123345
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            WindowViewModel windowViewModel = new WindowViewModel();
            windowViewModel.Status = "off";
            DataContext = windowViewModel;
        }

    }
}

当然有办法通过某种方式使用触发器来做到这一点。

【问题讨论】:

    标签: c# wpf xaml triggers


    【解决方案1】:

    也许您需要实现 INotifyPropertyChanged 并在 Status 更改时引发 PropertyChange ?

    您可以在可见性和状态字符串之间使用转换器,而不是使用触发器。

    【讨论】:

    • 所以我的转换器会接受一个字符串(状态)并返回一个属性可见性?还是您的意思是返回整个元素?这将如何在代码中实现?
    • 我也用 ViewModel(上面的代码)重新实现了这个,它得到了同样的错误。
    【解决方案2】:

    对于 Bindings 使用 DataTrigger,对于属性,您可以使用 Trigger.. 还要确保 Status 属性通知 ;) 使其成为依赖属性或使用 INotifyPropertyChanged 接口。

    DataTrigger on MSDN

    Nice article how to combine all these trigger goodness

    【讨论】:

    • 它与 DataTrigger 和依赖属性(上面发布的代码)出现相同的错误。
    • 我的代码似乎和那篇文章实现的一样,只是一个DataTrigger,绑定到一个属性,只是他使用了一种样式,但这应该没有任何区别。
    • 我也用 ViewModel(上面的代码)重新实现了它,它得到了同样的错误。它必须与 XAML 相关,我真的需要 EventTrigger 吗?所有代码示例(就像您提供的那样)都只是使用 DataTrigger 并绑定到他们想要检查的属性。
    • 嘿,爱德华,看看你其他问题的答案 :) stackoverflow.com/questions/914806/…
    【解决方案3】:

    您需要指定应设置可见性的类型

    <Setter Property="FrameworkElement.Visibility" Value="Visible"/>
    

    【讨论】:

    • 为什么有时没有类型名称也能工作?像 ...这令人困惑...
    • 因为微软,这就是原因。
    • A DependencyObject 可以在具有相同名称的父类上“隐藏”DependencyProperty,但它们需要在字典中具有唯一名称。在这种情况下,“FrameworkElement”。是字典键的一部分,这是你需要引用的。
    【解决方案4】:

    元素的触发器只支持EventTrigger,所以不能使用属性触发器(Trigger)。查看 FrameworkElement.Triggers 属性。

    【讨论】:

      【解决方案5】:

      试试这样的:

      <PasswordBox Name="pbxPassword" />
      <TextBox Text="{Binding Password,
                              ElementName=pbxPassword,
                              UpdateSourceTrigger=PropertyChanged}">
          <TextBox.Style>
              <Style TargetType="TextBox">
                  <Setter Property="Visibility" Value="Hidden" />
                  <Style.Triggers>
                      <DataTrigger Binding="{Binding IsChecked, ElementName=chbShowPassword}" Value="True">
                          <Setter Property="Visibility" Value="Visible" />
                      </DataTrigger>                  
                  </Style.Triggers>
              </Style>
          </TextBox.Style>
      </TextBox>
      <CheckBox Name="chbShowPassword">
          Show password
      </CheckBox>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-04
        • 2018-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-23
        • 2012-08-05
        • 2010-10-29
        相关资源
        最近更新 更多