C# 如何将textblock可见性绑定到另一个textblock文本属性
大家好,我有一个文本块,上面有一条线表示旧的价格。和另一个文本块,用于下一个文本之间的间距。但是,当没有dicount所以没有oldPrice值时,我将oldPrice文本设置为null 所以我也想隐藏间距文本块。是否有任何可能的xaml方法将最后一个TextBlock的可见性属性绑定到oldPrice的文本。因此,如果oldPrice文本为null或空字符串,它将不可见C# 如何将textblock可见性绑定到另一个textblock文本属性,c#,xaml,windows-phone-8,windows-phone-8.1,winrt-xaml,C#,Xaml,Windows Phone 8,Windows Phone 8.1,Winrt Xaml,大家好,我有一个文本块,上面有一条线表示旧的价格。和另一个文本块,用于下一个文本之间的间距。但是,当没有dicount所以没有oldPrice值时,我将oldPrice文本设置为null 所以我也想隐藏间距文本块。是否有任何可能的xaml方法将最后一个TextBlock的可见性属性绑定到oldPrice的文本。因此,如果oldPrice文本为null或空字符串,它将不可见 感谢使用转换器,您可以实现这一点 在xaml中 <Grid Height="{Binding ElementN
感谢使用转换器,您可以实现这一点 在xaml中
<Grid
Height="{Binding ElementName=oldPrice, Path=Height}">
<TextBlock
VerticalAlignment="Bottom"
FontSize="{StaticResource TextStyleSmallFontSize}"
RequestedTheme="Light"
FontWeight="Bold"
Foreground="#B0B0B0"
Style="{StaticResource TitleTextBlockStyle}"
TextWrapping="NoWrap">
<Run
x:Name="oldPrice"
Text="{Binding oldPrice}" />
</TextBlock>
<Line
Stretch="Fill"
Stroke="#B0B0B0"
StrokeThickness="1"
X1="1"
Width="{Binding ElementName=oldPrice, Path=Width}"
Height="{Binding ElementName=oldPrice, Path=Height}"
Margin="0,6,0,0" />
</Grid>
<TextBlock
Text="   "
FontSize="{StaticResource TextStyleMediumFontSize}"
RequestedTheme="Light"
Style="{StaticResource TitleTextBlockStyle}"
TextWrapping="NoWrap" />
如果您将可见性直接绑定到文本,则意味着它将显示文本,例如始终可见/隐藏。必须添加xaml才能识别它。它成功了。谢谢
<TextBlock x:Name="TB" Text="Text"/>
<TextBox Visibility="{Binding ElementName=TB,Path=Text,Converter={StaticResource StringToVisibilityConverter}}"/>
public class StringToVisibilityConverter: IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return string.IsNullOrEmpty((string)value)?Visibility.Collapsed:Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (Visibility)value == Visibility.Visible;
}
#endregion
}