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?
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:
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:
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.
You need to specify the Type on which the visibility should be set
<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). Look FrameworkElement.Triggers Property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With