FallbackValue如何与MultiBinding一起使用?

时间:2010-04-19 16:11:45

标签: wpf multibinding fallbackvalue

我问,因为它似乎不起作用。

假设我们绑定到以下对象:

public class HurrDurr
{
  public string Hurr {get{return null;}}
  public string Durr {get{return null;}}
}

好吧,看来如果我们使用 MultiBinding 来显示回退值,对吧?

<TextBlock>
    <TextBlock.Text>                                
        <MultiBinding StringFormat="{}{0} to the {1}"
                        FallbackValue="Not set!  It works as expected!)">
            <Binding Path="Hurr"/>
            <Binding Path="Durr"/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

然而,结果实际上是“到”。 即使强制绑定返回DependencyProperty.UnsetValue也不起作用:

<TextBlock xmnlns:base="clr-namespace:System.Windows;assembly=WindowsBase">
    <TextBlock.Text>                                
        <MultiBinding StringFormat="{}{0} to the {1}"
            FallbackValue="Not set!  It works as expected!)">
            <Binding Path="Hurr"
                FallbackValue="{x:Static base:DependencyProperty.UnsetValue}" />
            <Binding Path="Durr"
                FallbackValue="{x:Static base:DependencyProperty.UnsetValue}" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

尝试使用 TargetNullValue ,这也是一直在破坏。

所以似乎 MultiBinding 永远不会使用 FallbackValue 。这是真的,还是我错过了什么?


稍微麻烦一点,我发现转换器可以返回我需要的UnsetValue:

class MultiValueFailConverter : IMultiValueConverter
{
    public object Convert(
        object[] values, 
        Type targetType, 
        object parameter, 
        System.Globalization.CultureInfo culture)
    {
        if (values == null || 
            values.Length != 2 ||
            values.Any(x=>x == null))
            return System.Windows.DependencyProperty.UnsetValue;
        return values;
    }

    public object[] ConvertBack(
        object value, 
        Type[] targetTypes, 
        object parameter, 
        System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException("Too complex hurt brain.");
    }
}

然而,这似乎是一个肮脏的污秽黑客。我认为这样的场景会在框架中加以说明。但是,我在Reflector中找不到任何东西。

1 个答案:

答案 0 :(得分:8)

这是一个老问题,但可以使用一些解释。

来自FallbackValue documentation

  

如果出现以下情况,绑定会成功返回值:

     
      
  1. 绑定源的路径成功解析。
  2.   
  3. 值转换器(如果有)能够转换结果值。
  4.   
  5. 结果值对绑定目标(目标)属性有效。
  6.         

    如果1和2返回DependencyProperty.UnsetValue,则为目标属性   如果可用,则设置为FallbackValue的值。如果   没有FallbackValue,目标属性的默认值是   使用

在提供的示例中,绑定成功解析为HurrDurr属性。 Null是字符串的有效值,表示绑定有效。

换句话说,当绑定无法返回值时使用FallbackValue,并且在提供的示例中,绑定确实提供了有效值。

举例说明基于原始示例的以下每个片段:

示例1
Hurr和Durr属性绑定正确; null是一个有效值,永远不会看到FallbackValue。

<TextBlock>
    <TextBlock.Text>
        <MultiBinding FallbackValue="Binding is valid. I will never be seen." StringFormat="{}{0} to the {1}">
            <Binding Path="Hurr" />
            <Binding Path="Durr" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

示例2
Hurr和Durr属性没有正确绑定;将会看到FallbackValue。

<TextBlock>
    <TextBlock.Text>
        <MultiBinding FallbackValue="Binding paths are invalid. Look at me." StringFormat="{}{0} to the {1}">
            <Binding Path="xHurr" />
            <Binding Path="xDurr" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

示例3
如果一个绑定路径无效,则会看到FallbackValue。

<TextBlock>
    <TextBlock.Text>
        <MultiBinding FallbackValue="One binding path is invalid. Look at me." StringFormat="{}{0} to the {1}">
            <Binding Path="xHurr" />
            <Binding Path="Durr" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

示例4
与前面的示例一样,绑定是正确的,因此不会使用FallbackValue。此外,Binding父项的每个子MultiBinding属性的FallbackValue应该引用一个FallbackValue用于MultiBinding的目标属性,而不是用于子绑定。

<TextBlock xmlns:base="clr-namespace:System.Windows;assembly=WindowsBase">
    <TextBlock.Text>
        <MultiBinding FallbackValue="Binding is valid. I will never be seen." StringFormat="{}{0} to the {1}">
            <Binding FallbackValue="{x:Static base:DependencyProperty.UnsetValue}" Path="Hurr" />
            <Binding FallbackValue="{x:Static base:DependencyProperty.UnsetValue}" Path="Durr" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

示例5
即使Binding属性中没有提供路径,绑定仍然有效,因为绑定将使用它绑定的任何对象。

<TextBlock xmlns:base="clr-namespace:System.Windows;assembly=WindowsBase">
    <TextBlock.Text>
        <MultiBinding FallbackValue="Binding is still valid. I will never be seen." StringFormat="{}{0} to the {1}">
            <Binding FallbackValue="{x:Static base:DependencyProperty.UnsetValue}" />
            <Binding FallbackValue="{x:Static base:DependencyProperty.UnsetValue}" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

例6
最后,如果将转换器添加到任何Binding属性以强制使用UnsetValue,则会看到MultiBinding FallbackValue:

<强>转换器

internal class ForceUnsetValueConverter : IValueConverter
{
    #region Implementation of IValueConverter

    public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
    {
        return DependencyProperty.UnsetValue;
    }

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

    #endregion
}

<强> XAML

<TextBlock>
    <TextBlock.Text>
        <MultiBinding FallbackValue="Binding is valid, but look at me. I'm an UnsetValue." StringFormat="{}{0} to the {1}">
            <Binding Converter="{StaticResource ForceUnset}" Path="Hurr" />
            <Binding Path="Durr" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>
相关问题