gpt4 book ai didi

wpf - 处理 MultiBinding 中绑定(bind)的 bool 回退值

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

我有一个 MultiBinding转换器,它根据我的两个 Binding 字段是否都评估为 true 来确定控件的可见性。如果由于某种原因它无法达到这些 bool 属性中的任何一个,我想将它们设置为评估为真。例如:

我目前拥有的代码是:

           <MultiBinding Converter="{StaticResource AndToVisibilityConverter1}" FallbackValue="Visible">
<Binding Path="IsConnected" FallbackValue="true" />
<Binding Path="CurrentUser.IsConsumerOnlyAgent" Converter="{StaticResource InvertedBooleanConverter1}" FallbackValue="True" />
</MultiBinding>

代码运行良好,但是我在 IDE 的输出中收到错误消息,指示:
System.Windows.Data Error: 11 : Fallback value 'True' (type 'String') cannot be converted for use in 'Visibility' (type 'Visibility'). BindingExpression:Path=IsConnected; DataItem=null; target element is 'StackPanel' (Name=''); 

我已经确定了这个转换器并验证了它是 XAML 错误所在的位置。抱歉这里的无知,但是有没有办法将 FallbackValue 设置为这些绑定(bind)中的每一个,以便在无法获取设置路径时评估为“true”?提前致谢

更新:

这是我的可见性转换器的代码(我在整个应用程序的几个地方都使用了它,只是想添加回退值):
   internal class AndToVisibilityConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null)
return Visibility.Collapsed;

foreach (var value in values)
{
if (value == null || !(value is bool))
return Visibility.Collapsed;

if (!((bool) value))
return Visibility.Collapsed;
}

return Visibility.Visible;
}

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

最佳答案

FallbackValue值应该是 Visibility绑定(bind)失败时回退的值:

<Binding Path="CurrentUser.IsConsumerOnlyAgent" 
Converter="{StaticResource InvertedBooleanConverter1}"
FallbackValue="Visible" />

编辑

来自 FallbackValue documentation :

fallbackValue
An attribute or object element value of the same type as the target property. See that type's documentation for XAML usage information. That type may or may not support attribute syntax for its values, or may or may not support object element syntax (which requires a default constructor on that type). The target property type will therefore influence which syntax you use for the value of the FallbackValue property.



在这种情况下,MultiBinding 的目标属性是 Visibility 属性。即使子 MultiBinding 属性的值需要一个 bool,父 MultiBinding 也需要一个来自绑定(bind)的 Visiblity 值。这意味着每个 MultiBinding 属性的 FallbackValue 都应具有 Visibility 属性的 FallbackValue。

似乎有点违反直觉,但仔细考虑(IMO)时确实有意义,re:如果 MultiBindng 上没有转换器,则绑定(bind)的返回值应该是 Visibility 值。从等式中删除转换器,它可能有助于解决问题。

对于您的情况,跳过每个 MultiBinding 值的 FallbackValue 并像现在一样在 MultiBinding 上设置 FallbackValue。
<MultiBinding Converter="{StaticResource AndToVisibilityConverter1}" FallbackValue="Visible">
<Binding Path="IsConnected" />
<Binding Path="CurrentUser.IsConsumerOnlyAgent" Converter="{StaticResource InvertedBooleanConverter1}" />
</MultiBinding>

关于wpf - 处理 MultiBinding 中绑定(bind)的 bool 回退值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14226638/

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