Когда я пытаюсь скомпилировать следующий код, я получаю сообщение об ошибке Элемент 'Visibility' недействителен, поскольку у него нет подходящего имени типа.
Что мне нужно изменить, чтобы я мог заставить TextBlock исчезнуть , когда Status = off?
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
}
}
Я переделал это с помощью ViewModel со свойством Status, которое реализует INotifyPropertyChanged, и он получает ту же ошибку:
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;
}
}
}
Конечно, есть способ сделать это с помощью триггера.