问题
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>
The converter looks like:
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();
}
}
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' occurred in PresentationFramework.dll
'Set property 'System.Windows.Data.MultiBinding.Converter' threw an exception.'"
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.
回答1:
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.
来源:https://stackoverflow.com/questions/28752481/multibind-not-working-in-xaml-wpf