当我尝试编译以下代码时,我收到错误'可见性'成员无效,因为它没有合格的类型名称。
当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
}
}
我使用ViewModel重写它,该ViewModel具有实现INotifyPropertyChanged的属性Status,并且它得到同样的错误:
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;
}
}
}
当然有一种方法可以用触发器以某种方式做到这一点。
答案 0 :(得分:60)
您需要指定应在其上设置可见性的类型
<Setter Property="FrameworkElement.Visibility" Value="Visible"/>
答案 1 :(得分:6)
尝试这样的事情:
<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>
答案 2 :(得分:2)
元素的触发器仅支持EventTrigger,因此您无法使用属性触发器(Trigger)。查看FrameworkElement.Triggers属性。
答案 3 :(得分:0)
当状态发生变化时,你可能需要实现INotifyPropertyChanged并引发PropertyChange吗?
您可以在Visibility和Status字符串之间使用转换器,而不是使用触发器。
答案 4 :(得分:0)
对于Bindings,使用DataTrigger,对于可以使用Trigger的属性。还要确保Status属性通知;)使其成为依赖属性或使用INotifyPropertyChanged接口。