Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF绑定-空字符串的默认值_C#_Wpf_Xaml_Binding - Fatal编程技术网

C# WPF绑定-空字符串的默认值

C# WPF绑定-空字符串的默认值,c#,wpf,xaml,binding,C#,Wpf,Xaml,Binding,如果绑定的字符串为空,是否有标准方法为WPF绑定设置默认值或回退值 <TextBlock Text="{Binding Name, FallbackValue='Unnamed'" /> FallbackValue似乎只在Name为null时生效,但在设置为String时无效。Empty我的印象是绑定失败时提供值,绑定值为null时提供值 要执行所需操作,您需要一个转换器(可能带有参数)将空字符串转换为目标值,或者将逻辑放入视图模型中 我可能会使用这样的转换器(未经测试) 您可

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

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


FallbackValue
似乎只在
Name
为null时生效,但在设置为
String时无效。Empty

我的印象是绑定失败时提供值,绑定值为null时提供值

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

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


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

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*/ }

您应该为此创建一个转换器,它实现
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
只表示绑定所在的
窗口
/
UserControl
/
样式
。)


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>


我需要为每个绑定指定不同的默认字符串,但我可以将该字符串作为ConverterParameter传入。@Viv为什么绑定到Name属性而不是文本?@Andrzej huh Name是OP在问题中提到的属性。在[enter link description here][1][1]中有一个非常简单的答案:在[TextBox Default Value if Binding返回null][1][1]有一个非常简单的解决方案:我喜欢这种方法,因为它让我可以将默认值绑定到ViewModel中的另一个属性。谢谢:)你也可以使用
Name
和空字符串来代替
Name.length
,完全喜欢这种方法:)因为
Name
通常是
string
,那么
Name.length
应该是
Int32
类型。因此,无需指定
TargetNullValue
,因为表达式的计算结果永远不会为
null
,即使它的计算结果为空
FallbackValue
是在这里起作用的属性。这样做很难看。。。对于这样一件简单的事情,应该是一行。
<xxx.Resources>
<local:StringEmptyConverter x:Key="StringEmptyConverter" />
</xxx.Resources>
<TextBlock Text="{Binding Name, Converter={StaticResource StringEmptyConverter}, ConverterParameter='Placeholder Text'}" />
<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>