简体   繁体   English

如何将文本块可见性绑定到另一个文本块文本属性

[英]How to bind a textblock visibility to another textblock text property

<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="&#160;&#160;&#160;"
    FontSize="{StaticResource TextStyleMediumFontSize}"
    RequestedTheme="Light"
    Style="{StaticResource TitleTextBlockStyle}"
    TextWrapping="NoWrap" />

Hi all, i have a textblock and a line above it for oldPrice indication. 大家好,我有一个textblock和上面的一行用于oldPrice指示。 And another textblock for spacing between next text. 另一个文本块用于下一个文本之间的间隔。 However when there is no dicount so no oldPrice value i am setting the oldPrice text to null. 但是,当没有dicount从而没有oldPrice值时,我会将oldPrice文本设置为null。

So i want to hide that spacing textblock too. 所以我也想隐藏该间距文本块。 Is there any possible xaml way to bind the last TextBlock's visibility property to oldPrice's text. 是否有任何可能的xaml方法将最后一个TextBlock的可见性属性绑定到oldPrice的文本。 So it will be invisible if oldPrice text is null or empty string. 因此,如果oldPrice文本为null或为空字符串,则它将不可见。

Thanks 谢谢

Using Converters You can achieve this 使用转换器您可以实现

In xaml 在xaml中

 <TextBlock x:Name="TB" Text="Text"/>
 <TextBox Visibility="{Binding ElementName=TB,Path=Text,Converter={StaticResource StringToVisibilityConverter}}"/>

And Corresponding converter in c# code is C#代码中对应的转换器是

 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
}

If you Bind Visibility to text directly means,It will Show the text something Like Visible/Hidden always. 如果直接将可见性绑定到文本意味着,它将始终显示类似可见/隐藏的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM