gpt4 book ai didi

wpf - FallbackValue 如何与 MultiBinding 一起使用?

转载 作者:行者123 更新时间:2023-12-04 04:08:30 27 4
gpt4 key购买 nike

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

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

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

好吧,看起来如果我们使用 多重绑定(bind) 与此相反,将显示后备值,对吗?
<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>

然而结果实际上是 “到” .
甚至强制绑定(bind)返回 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>

进行了同样的尝试目标空值 ,这也是一个半身像。

所以看来 多重绑定(bind) 永远不会使用 后备值 .这是真的,还是我错过了什么?

稍微搞砸了,我发现转换器可以返回我需要的 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 中找不到任何东西。

最佳答案

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

来自 FallbackValue documentation :

A binding returns a value successfully if:

  1. The path to the binding source resolves successfully.
  2. The value converter, if any, is able to convert the resulting value.
  3. The resulting value is valid for the binding target (target) property.

If 1 and 2 return DependencyProperty.UnsetValue, the target property is set to the value of the FallbackValue, if one is available. If there is no FallbackValue, the default value of the target property is used.



在提供的示例中,绑定(bind)成功解析为 HurrDurr特性。 Null 是字符串的有效值,这意味着绑定(bind)有效。

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

以以下每个基于原始示例的片段为例:

示例 1
Hurr 和 Durr 属性绑定(bind)正确; 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 属性未正确绑定(bind);将看到 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
如果一个绑定(bind)路径无效,则会看到 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
与前面的示例一样,绑定(bind)是正确的,因此不会使用 FallbackValue。此外,每个子 Binding 的 FallbackValue MultiBinding 的属性parent 应该引用用于 MultiBinding 的目标属性的 FallbackValue,而不是子 Bindings。
<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 中没有提供路径,绑定(bind)仍然有效属性,因为绑定(bind)将使用它绑定(bind)到的任何对象。
<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>

关于wpf - FallbackValue 如何与 MultiBinding 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2668987/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com