Reputation: 193282
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.
Upvotes: 22
Views: 28244
Reputation: 93
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>
Upvotes: 6
Reputation: 56
Triggers of an element only support EventTrigger so you can't use property triggers (Trigger). Look FrameworkElement.Triggers Property.
Upvotes: 2
Reputation: 601
You need to specify the Type on which the visibility should be set
<Setter Property="FrameworkElement.Visibility" Value="Visible"/>
Upvotes: 60
Reputation: 27055
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.
Nice article how to combine all these trigger goodness
Upvotes: 0
Reputation: 2773
Maybe you need to implement INotifyPropertyChanged and raise PropertyChange when Status change ?
Instead of using a trigger, you can use a Converter between Visibility and your Status string.
Upvotes: 0