簡體   English   中英

如何使用觸發器更改TextBlock的可見性?

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

當我嘗試編譯以下代碼時,我得到錯誤“可見性”成員無效,因為它沒有合格的類型名稱。

我需要更改什么才能在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;
        }

    }
}

當然有一種方法可以用觸發器以某種方式做到這一點。

您需要指定應在其上設置可見性的類型

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

嘗試這樣的事情:

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

元素的觸發器僅支持EventTrigger,因此您無法使用屬性觸發器(Trigger)。 查看FrameworkElement.Triggers屬性。

也許您需要實現INotifyPropertyChanged並在Status更改時引發PropertyChange?

您可以在Visibility和Status字符串之間使用Converter,而不是使用觸發器。

對於Bindings,使用DataTrigger,對於可以使用Trigger的屬性。還要確保Status屬性通知;)使其成為依賴屬性或使用INotifyPropertyChanged接口。

MSDN上的DataTrigger

好文章如何結合所有這些觸發優點

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM