C# 如何使用触发器更改 TextBlock 的可见性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/914660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:05:02  来源:igfitidea点击:

How can I change the Visibility of a TextBlock with a Trigger?

c#wpfxamltriggers

提问by Edward Tanguay

When I try to compile the following code, I get the error 'Visibility' member is not valid because it does not have a qualifying type name.

当我尝试编译以下代码时,我收到错误“可见性”成员无效,因为它没有限定类型名称。

What do I have to change so that I can make the TextBlock disappearwhen Status=off?

我必须更改什么才能在 Status=off 时使 TextBlock 消失

XAML:

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>

Code Behind:

背后的代码:

using System.Windows;

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

        public string Status { get; set; }

    }
}

I changed to DataTrigger and Dependency Properties and it gets the same error:

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

XAML:

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>

Code Behind:

背后的代码:

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


    }
}

I redid this with a ViewModel that has a property Status that implements INotifyPropertyChanged, and it gets that same error:

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

WindowViewModel.cs:

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

Code Behind:

背后的代码:

using System.Windows;

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

    }
}

Surely there is a way to do this with a trigger somehow.

当然,有一种方法可以通过触发器以某种方式做到这一点。

回答by rockeye

Maybe you need to implement INotifyPropertyChanged and raise PropertyChange when Status change ?

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

Instead of using a trigger, you can use a Converter between Visibility and your Status string.

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

回答by Arcturus

For Bindings use DataTrigger, for properties you can use Trigger.. Also make sure the Status Property notifies ;) Either make it a dependency property or use the INotifyPropertyChanged interface.

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

DataTrigger on MSDN

MSDN 上的数据触发器

Nice article how to combine all these trigger goodness

好文章如何结合所有这些触发善良

回答by Will Perry

You need to specify the Type on which the visibility should be set

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

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

回答by Tecky

Triggers of an element only support EventTrigger so you can't use property triggers (Trigger). Look FrameworkElement.Triggers Property.

元素的触发器仅支持 EventTrigger,因此您不能使用属性触发器(Trigger)。看看 FrameworkElement.Triggers 属性。

回答by Yuri Perekupko

Try something like this:

尝试这样的事情:

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