我正在尝试将 TextBlock 的可见性链接到 bool 属性,该属性还使用 WPF 和 C# 链接到复选框。我在同一个 xaml 文件的两个不同部分中有以下代码(一个部分是摘要,另一个是设置。我是 WPF 的新手,并且正在学习。目前,TextBlock 无论如何都是可见的IsSecondaryMessageFilePath 的值为。
<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;
如有任何帮助,我们将不胜感激。
编辑
根据下面的建议,我尝试添加 BooleanToVisibilityConverter,但我得到了一个缺少的程序集引用,我是 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"/>
...
最佳答案
您的代码乍一看没问题,但实际上您不需要为此使用数据触发器。 WPF 带有您在资源中声明的 BooleanToVisibilityConverter 类:
<BooleanToVisibiltyConverter x:Key="BooleanToVisibilityConverter"/>
然后在您的 TextBlock 中,绑定(bind)可见性:
<TextBlock Visibility="{Binding Path=IsSecondaryMessageFilePath, Converter={StaticResource BooleanToVisibilityConverter}}"/>
如您所知,可能有更简单的方法来执行此操作,只需绑定(bind)到 IsChecked 属性本身!
<CheckBox x:Name="UseSecondaryPath"/>
<TextBlock Visibility="{Binding ElementName=UseSecondaryPath, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}"/>
当然,如果您需要 bool 来做其他事情,这不是一个理想的解决方案,但如果它只用于 UI,它会更简洁一些。
如果您有兴趣,自定义 BooleanToVisibilityConverter 的代码是:
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;
}
}
如果我可以澄清任何事情或进一步提供帮助,请告诉我。
关于c# - 基于装订书籍设置 TextBlock 可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22676437/