【问题标题】:Conditional text binding XAML条件文本绑定 XAML
【发布时间】:2015-03-02 18:49:28
【问题描述】:

我有 3 个属性正在尝试绑定到 XAML 中的 Textblock。一个是条件,另外两个是我要根据该条件显示的字符串。

<TextBlock Text="{Binding TrueText}" Style="{StaticResource styleSimpleText}" Visibility="{Binding ShowTrueText, Converter={StaticResource boolToVisibilityConverter}}"/>
<TextBlock Text="{Binding FalseText}" Style="{StaticResource styleSimpleText}" Visibility="{Binding ShowTrueText, Converter={StaticResource invertedBoolToVisibilityConverter}}"/>

这可行,但现在文本块必须具有不同的名称。我可以把它变成一个带有条件的 TextBlock 吗?

【问题讨论】:

  • 您需要将它们绑定到您的 cs 类中的成员并实现 OnPropertyChanged 或一些方法来处理/引发属性更改事件:public string thisText { get { if ("conditions) { return "this text"; } else { return "this other text";} }//end of get }end of member 然后您的 xaml 元素将看起来链接此:@ 987654324@和OnPropertyChanged.上的一些资源

标签: c# wpf xaml


【解决方案1】:

您可以使用 Style 和 DataTrigger 来实现:

<TextBlock>
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Setter Property="Text" Value="{Binding FalseText}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ShowTrueText}" Value="True">
                    <Setter Property="Text" Value="{Binding TrueText}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

另一种方法是将 MultiBinding 与多值转换器一起使用:

<TextBlock>
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource TextConverter}">
            <Binding Path="TrueText"/>
            <Binding Path="FalseText"/>
            <Binding Path="ShowTrueText"/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

转换器如下所示:

public class TextConverter : IMultiValueConverter
{
    public object Convert(
        object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var trueText = (string)values[0];
        var falseText = (string)values[1];
        var showTrueText = (bool)values[2];
        return showTrueText ? trueText : falseText;
    }

    public object[] ConvertBack(
        object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

【讨论】:

    【解决方案2】:

    您可以在视图模型中设置它并让它决定要显示的文本。

    private static readonly string TRUETEXT = "This is the text to show when true";
        private static readonly string FALSETEXT = "This is the text to show when false";
    
        private bool _myBooleanProperty;
        public bool MyBooleanProperty
        {
            get { return _myBooleanProperty; }
            set
            {
                if (_myBooleanProperty != value)
                {
                    _myBooleanProperty = value;
                    OnPropertyChanged("MyBooleanProperty");
                    OnPropertyChanged("ResultText");
                }
            }
        }
    
        public string ResultText
        {
            get
            {
                return MyBooleanProperty ? TRUETEXT : FALSETEXT;
            }
        }
    

    然后你只用一个文本块绑定到它。无需可见性转换器。
    如果存在不应该显示文本的状态,您也可以这样做。

    <TextBlock Text="{Binding ResultText}" Style="{StaticResource styleSimpleText}" />
    

    【讨论】:

      【解决方案3】:

      我们为 MVVM 做这类事情的方式是为此在您的视图模型中创建一个属性。这允许您对视图模型上的条件进行单元测试。

      视图模型中的属性将是 TextBlock 绑定到的字符串值。视图模型在某些时候会根据您需要的条件逻辑确定该字符串的值。

      【讨论】:

        【解决方案4】:

        是的,你可以,只需将它们包装在 TextBlock 中,如下所示:

        <TextBlock x:name="myTextBlock" Style="{StaticResource styleSimpleText}">
            <TextBlock Text="{Binding TrueText}" Visibility="{Binding ShowTrueText, Converter={StaticResource boolToVisibilityConverter}}"/>
            <TextBlock Text="{Binding FalseText}" Visibility="{Binding ShowTrueText, Converter={StaticResource invertedBoolToVisibilityConverter}}"/>
        </TextBlock>
        

        但是,我认为最好的答案是 Clemens 提供的答案(使用 DataTrigger)。

        【讨论】:

          【解决方案5】:

          在我看来,这个问题的最佳解决方案是在您的视图模型中添加一个新的字符串属性,根据条件返回TrueTextFalseText。有了这样的属性,您就可以使用普通绑定。

          public string TheNewProperty
          {
              get
              {
                  return ShowTrueText ? TrueText : FalseText;
              }
          }
          
          <TextBlock Text="{Binding TheNewProperty}" Style="{StaticResource styleSimpleText}"/>
          

          【讨论】:

          • 我正在尝试将它与 MenuItem 一起使用,但标题没有改变。&lt;MenuItem Header="{Binding PlayOrPauseLabel}" Command="{Binding TimerPlayPauseCommand}"/&gt; 这是一个专业吗?
          • @Manticore 虽然已经很晚了,而且您不需要答案,但它可能会对其他人有所帮助。当引发“ShowTrueText”或“TrueText”或“FalseText”时,您需要引发“TheNewProperty”上的属性更改。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-06-06
          • 1970-01-01
          • 2016-01-17
          • 2011-01-22
          • 1970-01-01
          • 1970-01-01
          • 2013-04-01
          相关资源
          最近更新 更多