【发布时间】:2020-05-20 02:07:42
【问题描述】:
我想根据后面代码的布尔变量的状态,用 Binding 改变 TextBlock 的可见性
<TextBlock Text="Hello">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsVisible}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
这不起作用:(
private bool _isVisible = false;
public bool IsVisible
{
get { return _isVisible; }
set
{
_isVisible = value;
NotifyPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
【问题讨论】:
-
您以前与
inotifypropertyhanged合作过吗?如果没有,那么我邀请您阅读它。 -
看不懂,能不能用代码给我解释一下?
-
你的变量名在这里错了:
public bool isVisible,应该是IsVisible -
@Cfun 是对的。这是您应该使用
nameof和/或CallerMemberName的原因之一。
标签: c# wpf data-binding