Multibind 在 XAML/WPF 中不起作用 [英] Multibind not working in XAML/WPF

查看:23
本文介绍了Multibind 在 XAML/WPF 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算 ItemsControl WPF 项目的宽度,但是我需要传递的不是一个而是两个参数.这是它的样子:

I need to calculate the width of an ItemsControl WPF item, however I need to pass not one but two parameters. Here is what it looks like:

<ItemsControl.Width>
   <MultiBinding Converter="{StaticResource animationKeyPositionConverter}">
      <Binding Path="CurrentFrame" ElementName="UC" />
      <Binding Path="CurrentZoom" ElementName="UC" />
   </MultiBinding>
</ItemsControl.Width>

转换器看起来像:

internal class AnimationKeyPositionConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var frame = System.Convert.ToInt32(values[0]);
        var zoom = System.Convert.ToDouble(values[1]);

        return (double)(frame * zoom);
    }

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

此转换器与另一个多绑定(使用 Setter,另一个控件)一起工作,但是它不能与 ItemsControl.Width 一起工作.有什么理由吗?当达到多绑定时,它会崩溃:

This converter worked with another multibind (using Setter, another control), however it will not work with ItemsControl.Width. Any reason why? When the multibind is reached, it crashes with:

"'System.Windows.Markup.XamlParseException'出现在 PresentationFramework.dll 中

"'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

'设置属性 'System.Windows.Data.MultiBinding.Converter' 引发异常.'"

'Set property 'System.Windows.Data.MultiBinding.Converter' threw an exception.'"

我找不到有关异常的更多信息,调试器输出仅说明在第 266 行(具有转换器属性的多绑定的第一行)抛出异常.再次值得注意的是,这个完全相同的 multibind 转换器和语法在另一个 xaml 控件中工作,但是一旦我将它放入 ItemsControl.Width 它开始抛出异常并崩溃.

I cannot find any more info on the exception, the debugger output only states that at line 266 (the first line of the multibind with the converter attribute) threw an exception. It is worth noting again that this exact same multibind converter and syntax worked in another xaml control, but once I put it in ItemsControl.Width it started throwing exceptions and crashing.

我在转换器上设置了一个断点,但代码甚至没有被访问.

I set a breakpoint at the converter and the code isn't even being reached.

推荐答案

我已经通过更改我使用的绑定路径解决了我的问题.我不确定为什么 CurrentFrame 和 CurrentZoom 一起抛出错误,但使用不同的路径(特别是为 CurrentZoom 指定一个相对源)我能够得到我需要的结果.转换器并不是真正的问题,尽管将 (double) 转换为 System.Convert.ToDouble 摆脱了 InnerException.我还将 ToInt32 转换也更改为双精度.

I have solved my issue by changing the binding paths I was using. I'm not sure why CurrentFrame and CurrentZoom threw an error together but using different paths (specifically, specifying a relativesource for CurrentZoom) I was able to get the results I needed. The converter wasn't really the issue, although changing the (double) casting to System.Convert.ToDouble got rid of the InnerException. I also changed the ToInt32 conversion to a double as well.

这篇关于Multibind 在 XAML/WPF 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆