[英]Multibinding with converter on one of the bindings in .net MAUI
My issue is that when I use a converter on one of the multibinding's binding.我的问题是,当我在多重绑定的绑定之一上使用转换器时。 It doesn't send the right thing to the converter.它不会向转换器发送正确的内容。 As per the DOC( https://learn.microsoft.com/en-us/do.net/maui/fundamentals/data-binding/multibinding?view.net-maui-7.0 ) in the Consume a IMultiValueConverter it should work, so I don't know what I'm doing wrong...根据使用 IMultiValueConverter 中的 DOC( https://learn.microsoft.com/en-us/do.net/maui/fundamentals/data-binding/multibinding?view.net-maui-7.0 ),它应该可以工作,所以我不知道我做错了什么......
My multibinding class is the following:我的多重绑定 class 如下:
public class BooleanAndConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null || !targetType.IsAssignableFrom(typeof(bool)))
{
return false;
}
foreach (var value in values)
{
if (!(value is bool b))
{
return false;
}
else if (!b)
{
return false;
}
}
return true;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
I have a Boolean inverter class which is the following:我有一个 Boolean 逆变器 class 如下:
public class InverseBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (targetType != typeof(bool))
throw new InvalidOperationException("The target must be a boolean");
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
Then in my XAML I'm using it like so:然后在我的 XAML 中,我像这样使用它:
<Button Text="Passer à la ronde suivante" Command="{Binding NextRoundCommand}">
<Button.IsVisible>
<MultiBinding Converter="{StaticResource BooleanAndConverter}">
<Binding Path="isGameStarted"/>
<Binding Path="isPlayersPlaying" Converter="{StaticResource InvertedBoolConverter}"/>
</MultiBinding>
</Button.IsVisible>
</Button>
When the inverter converter gets called in the multibinding, instead of receiving a TagetType of bool, it receive a "System.object", so it throws the InvalidOperationException.当在多重绑定中调用逆变器转换器时,它接收的不是布尔型 TagetType,而是“System.object”,因此它会抛出 InvalidOperationException。 Why when using normal bindings it receive a targettype of bool and in multibinding it doesn't?为什么在使用普通绑定时它会收到 bool 的目标类型,而在多重绑定中却不会?
Thanks谢谢
I couldn't see other code, but I tested the sample code DataBindingDemos , it works on my side.我看不到其他代码,但我测试了示例代码DataBindingDemos ,它在我这边有效。
But I found that you didn't implement function ConvertBack
for both of your ( BooleanAndConverter
and InverseBooleanConverter
).但我发现你没有为你的两个( BooleanAndConverter
和InverseBooleanConverter
)实现 function ConvertBack
。
You can refer to the following code:您可以参考以下代码:
public class InverterConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool? b = value as bool?;
if (b == null)
{
return false;
}
return !b.Value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Convert(value, targetType, parameter, culture);
}
}
public class AllTrueMultiConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null || !targetType.IsAssignableFrom(typeof(bool)))
{
return false;
// Alternatively, return BindableProperty.UnsetValue to use the binding FallbackValue
}
foreach (var value in values)
{
if (!(value is bool b))
{
return false;
// Alternatively, return BindableProperty.UnsetValue to use the binding FallbackValue
}
else if (!b)
{
return false;
}
}
return true;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
if (!(value is bool b) || targetTypes.Any(t => !t.IsAssignableFrom(typeof(bool))))
{
// Return null to indicate conversion back is not possible
return null;
}
if (b)
{
return targetTypes.Select(t => (object)true).ToArray();
}
else
{
// Can't convert back from false because of ambiguity
return null;
}
}
}
For more information, you can check: AllTrueMultiConverter.cs and InverterConverter.cs更多信息,您可以查看: AllTrueMultiConverter.cs和InverterConverter.cs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.