设置TextBlock可见度基于绑定bool

本文关键字:绑定 bool 于绑定 TextBlock 可见度 设置 | 更新日期: 2023-09-27 17:54:19

我试图将TextBlock的可见性链接到bool属性,该属性也链接到使用WPF和c#的复选框。我在同一个xaml文件的两个不同部分中有以下代码(一个部分是摘要,另一个部分是设置)。我是WPF的新手,边走边学习。目前,无论IsSecondaryMessageFilePath的值是多少,TextBlock都是可见的。

<TextBlock Name="secondaryfolderinfo" Foreground="Red">
    <ContentControl Content="Secondary message folder" Foreground ="Black" />                    
    <ContentControl Content = "{Binding Path=SecondaryMessageFilePath}" ContentStringFormat="" ClipToBounds="False"></ContentControl>
    <ContentControl Content = "   "></ContentControl>
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsSecondaryMessageFilePath}" Value="True">
                    <Setter Property="Visibility" Value="Visible"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

下面是:

<CheckBox IsChecked="{Binding Path=IsSecondaryMessageFilePath, Mode=TwoWay}"
    Name="SecondaryPathCheckBox"
    VerticalAlignment="Top"
    HorizontalAlignment="Left"
    Margin="320,7,0,0">Save additional locations</CheckBox>

最后,在代码后面,我有:

public bool IsSecondaryMessageFilePath
{
    get { return _isSecondaryMessageFilePath; }
    set
    {
        if (_isSecondaryMessageFilePath != value)
        {
            _isSecondaryMessageFilePath = value;
            OnPropertyChanged("IsSecondaryMessageFilePath");
        }
    }
}
private bool _isSecondaryMessageFilePath;
public string SecondaryMessageFilePath
{
    get { return _secondaryMessageFilePath; }
    set
    {
        if (_secondaryMessageFilePath != value)
        {
            _secondaryMessageFilePath = value;
            OnPropertyChanged("SecondaryMessageFilePath");
        }
    }
}
private string _secondaryMessageFilePath;

如有任何帮助,不胜感激。

编辑

从下面的建议工作,我试着添加boolean可见度转换器,但我得到了一个缺失的汇编参考,我是新的WPF找出如何解决它。我的开始代码如下:

    <UserControl x:Class="Sender_Receiver.SenderReceiverSetup"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    xmlns:m=...
    xmlns:
    <UserControl.Resources>
        <BooleanToVisibiltyConverter x:Key="BooleanToVisibilityConverter"/>
...

设置TextBlock可见度基于绑定bool

您的代码乍一看看起来不错,但实际上并不需要为此使用数据触发器。WPF附带了一个boolean可见度转换器类,你可以在你的资源中声明:

<BooleanToVisibiltyConverter x:Key="BooleanToVisibilityConverter"/>

然后在TextBlock中绑定Visibility:

<TextBlock Visibility="{Binding Path=IsSecondaryMessageFilePath, Converter={StaticResource BooleanToVisibilityConverter}}"/>

只是让你知道,可能有一个更简单的方法来做到这一点,只是绑定到IsChecked属性本身!

<CheckBox x:Name="UseSecondaryPath"/>
<TextBlock Visibility="{Binding ElementName=UseSecondaryPath, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}"/>

当然,如果你需要bool用于其他用途,这不是一个理想的解决方案,但如果它只是用于UI,它会更简洁一些。

自定义boolean可视性转换器的代码,如果你感兴趣,是:

public class BooleanToVisibilityConverter : IValueConverter
{
   public object Convert (object value, ...)
   {
       if ((bool)value)
          return Visibility.Visible;
       else
          return Visibility.Collapsed;
   }
   public object ConvertBack(object value, ...)
   {
      return Binding.DoNothing;
   }
}
private Boolean _IsChecked;
//Bind this to your checkbox
public Boolean IsChecked
{
    get { return _IsChecked; }
    set { _IsChecked= value; OnPropertyChanged("IsChecked"); OnPropertyChanged("TextBoxVis"); }
}

//Bind this to your TextBox's Visibility Property
public Visibility TextBoxVis
{
    get { return IsChecked ? Visibility.Visible : Visibility.Collapsed; }
}