【问题标题】:WPF Binding - Default value for empty stringWPF 绑定 - 空字符串的默认值
【发布时间】:2013-03-12 03:31:47
【问题描述】:

如果绑定字符串为空,是否有标准方法可以为 WPF 绑定设置默认值或回退值?

<TextBlock Text="{Binding Name, FallbackValue='Unnamed'" />

FallbackValue 似乎只在Name 为空时才生效,但在设置为String.Empty 时不会生效。

【问题讨论】:

标签: c# wpf xaml binding


【解决方案1】:

DataTrigger 是我这样做的方式:

<TextBox>
  <TextBox.Style>
        <Style TargetType="{x:Type TextBox}"  BasedOn="{StaticResource ReadOnlyTextBox}">
            <Setter Property="Text" Value="{Binding Name}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Name.Length, FallbackValue=0, TargetNullValue=0}" Value="0">
                    <Setter Property="Text" Value="{x:Static local:ApplicationLabels.NoValueMessage}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

【讨论】:

  • 我喜欢这种方法,因为它让我可以将默认值绑定到我的 ViewModel 中的另一个属性。谢谢:)
  • 你也可以用Name 和空字符串代替Name.length,非常喜欢这种方法:)
  • 看到Name 通常是string,那么Name.Length 将是Int32 类型。出于这个原因,不需要指定TargetNullValue,因为表达式永远不会计算为null,因为它完全可以计算。 FallbackValue 是这里最重要的属性。
  • 丑陋的方式来做到这一点......对于这样一个简单的事情应该是 1 班轮。
【解决方案2】:

我的印象是FallbackValue在绑定失败时提供一个值,而TargetNullValue在绑定值为空时提供一个值。

要执行您想要的操作,您需要一个转换器(可能带有参数)将空字符串转换为目标值,或者将逻辑放入您的视图模型中。

我可能会使用类似这样的转换器(未经测试)。

public class EmptyStringConverter : MarkupExtension, IValueConverter
{  
    public object Convert(object value, Type targetType, 
                          object parameter, CultureInfo culture)
    {
        return string.IsNullOrEmpty(value as string) ? parameter : value;
    }

    public object ConvertBack(object value, Type targetType, 
                              object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

【讨论】:

    【解决方案3】:

    你应该为此创建一个转换器,它实现了IValueConverter

    public class StringEmptyConverter : IValueConverter {
    
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
          return string.IsNullOrEmpty((string)value) ? parameter : value;
        }
    
    public object ConvertBack(
          object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
          throw new NotSupportedException();
        }
    
    }
    

    然后在 xaml 中,您只需将转换器提供给绑定,(xxx 仅代表您的 Window / UserControl / Style ... 绑定所在的位置)

    <xxx.Resources>
    <local:StringEmptyConverter x:Key="StringEmptyConverter" />
    </xxx.Resources>
    <TextBlock Text="{Binding Name, Converter={StaticResource StringEmptyConverter}, ConverterParameter='Placeholder Text'}" />
    

    【讨论】:

    • 我需要为每个绑定指定不同的默认字符串,但我可以将字符串作为 ConverterParameter 传递。
    • @Viv 为什么绑定到 Name 属性,而不是 Text?
    • @Andrzej huh Name 是 OP 在他的问题中提到的属性。
    【解决方案4】:

    您可以使用转换器并对其进行相应的验证。

    Binding="{Binding Path=Name, Converter={StaticResource nameToOtherNameConverter}}"
    

    在你的转换器中

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!string.IsNullOrEmpty(value.ToString()))
            { /*do something and return your new value*/ }
    

    【讨论】:

      猜你喜欢
      • 2011-01-15
      • 2010-11-08
      • 2018-11-25
      • 2015-08-23
      • 2011-07-22
      • 2020-07-27
      • 1970-01-01
      • 2014-03-05
      • 2022-01-22
      相关资源
      最近更新 更多