简体   繁体   English

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

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

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 disappear when 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. 当然有一种方法可以用触发器以某种方式做到这一点。

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

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

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>

Triggers of an element only support EventTrigger so you can't use property triggers (Trigger). 元素的触发器仅支持EventTrigger,因此您无法使用属性触发器(Trigger)。 Look FrameworkElement.Triggers Property. 查看FrameworkElement.Triggers属性。

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. 您可以在Visibility和Status字符串之间使用Converter,而不是使用触发器。

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. 对于Bindings,使用DataTrigger,对于可以使用Trigger的属性。还要确保Status属性通知;)使其成为依赖属性或使用INotifyPropertyChanged接口。

DataTrigger on MSDN MSDN上的DataTrigger

Nice article how to combine all these trigger goodness 好文章如何结合所有这些触发优点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM