WrapPanel ItemWidth & ItemHeight 多重绑定

WrapPanel ItemWidth & ItemHeight MultiBinding

我只想将 MultiBinding 用于 WrapPanelItemHeightItemWidth。代码是这样的:

<Window.Resources>
    <local:SensorHeightCalculator x:Key="HeightCalculator"/>
    <local:SensorWidthCalculator x:Key="WidthCalculator"/>
</Window.Resources>

<Border x:Name="sensorPanelBorder" BorderBrush="#FFD5DFE5" BorderThickness="1" Grid.Column="2" Margin="0,9,2,2" CornerRadius="3">
    <ListView x:Name="sensorPanel" Margin="0" ItemsSource="{Binding Source={StaticResource SensorControls}}">
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel x:Name="sensorWrapPanel" IsItemsHost="True">
                    <WrapPanel.ItemHeight>
                        <MultiBinding Converter="{StaticResource HeightCalculator}" UpdateSourceTrigger="PropertyChanged">
                            <Binding ElementName="sensorPanelBorder" Path="ActualHeight"/>
                            <Binding ElementName="sensorPanelBorder" Path="ActualWidth"/>
                            <Binding ElementName="sensorPanel" Path="Items.Count"/>
                        </MultiBinding>
                    </WrapPanel.ItemHeight>
                </WrapPanel>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
    </ListView>
</Border>

但是它抛出异常并且不渲染。 我也尝试在代码隐藏中执行此操作,但这也没有用。

实际问题是我需要将WrapPanel的项绑定到一个CollectionViewSource,因此,我在网上看的时候,我必须在一个里面使用WrapPanel ListView(如上)。在那之前,我手动填充了 WrapPanel 我有一个方法,我用来计算 WrapPanelItemHeightItemWidth 并分配给它。但是现在 WrapPanel 位于 ListView 内,无法在代码隐藏中访问它,因此我决定使用 Multibinding.

SensorHeightCalculator 来源:

public class SensorHeightCalculator : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            double H = (double)values[0];
            double W = (double)values[1];
            double N = (double)values[2];
            if (N > 0)
            {
                double k = 7.0 / 6.0;
                double c = N;
                double r = 1;
                double ah, aw, a, b;
                do
                {
                    aw = (W - 2) / c;
                    ah = k * aw;
                    if (Math.Floor(H / ah) <= r) break;
                    else
                    {
                        r++;
                        c = c - Math.Floor(c / r);
                    }
                } while (r <= N);
                a = Math.Min(aw, H / (k * r));
                b = k * a;
                return b - 10;
            }
            else
                return 300;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            return default(object[]);
        }
    }

异常和完整堆栈跟踪:

异常:InvalidCastException:指定的转换无效。

堆栈跟踪:

at AvaPa.SensorHeightCalculator.Convert(Object[] 值,类型 targetType,对象参数,CultureInfo 文化) 在 System.Windows.Data.MultiBindingExpression.TransferValue() 在 System.Windows.Data.MultiBindingExpression.Transfer() 在 System.Windows.Data.MultiBindingExpression.UpdateTarget(布尔值 includeInnerBindings) 在 System.Windows.Data.MultiBindingExpression.AttachToContext(布尔最后机会) 在 System.Windows.Data.MultiBindingExpression.AttachOverride(DependencyObject d, DependencyProperty dp) 在 System.Windows.Data.BindingExpressionBase.OnAttach(DependencyObject d, DependencyProperty dp) 在System.Windows.StyleHelper.GetInstanceValue(UncommonField1 dataField, DependencyObject container, FrameworkElement feChild, FrameworkContentElement fceChild, Int32 childIndex, DependencyProperty dp, Int32 i, EffectiveValueEntry& entry) at System.Windows.StyleHelper.GetChildValueHelper(UncommonField1 dataField, ItemStructList1& valueLookupList, DependencyProperty dp, DependencyObject container, FrameworkObject child, Int32 childIndex, Boolean styleLookup, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot) at System.Windows.StyleHelper.GetChildValue(UncommonField1 dataField, DependencyObject container, Int32 childIndex, FrameworkObject child, DependencyProperty dp, FrugalStructList1& childRecordFromChildIndex, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot) at System.Windows.StyleHelper.GetValueFromTemplatedParent(DependencyObject container, Int32 childIndex, FrameworkObject child, DependencyProperty dp, FrugalStructList1& childRecordFromChildIndex, FrameworkElementFactory templateRoot, EffectiveValueEntry&入口) 在 System.Windows.StyleHelper.ApplyTemplatedParentValue(DependencyObject 容器,FrameworkObject 子项,Int32 childIndex,FrugalStructList1& childRecordFromChildIndex, DependencyProperty dp, FrameworkElementFactory templateRoot) at System.Windows.StyleHelper.InvalidatePropertiesOnTemplateNode(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList1& childRecordFromChildIndex,布尔值 isDetach,FrameworkElementFactory templateRoot) 在 System.Windows.FrameworkTemplate.InvalidatePropertiesOnTemplate(DependencyObject 容器,对象 currentObject) 在 System.Windows.FrameworkTemplate.HandleBeforeProperties(对象 createdObject、DependencyObject 和 rootObject、DependencyObject 容器、FrameworkElement feContainer、INameScope nameScope) 在 System.Windows.FrameworkTemplate.<>c__DisplayClass45_0.b__2(对象发送者,XamlObjectEventArgs args) 在 System.Xaml.XamlObjectWriter.OnBeforeProperties(对象值) 在 System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx) 在 System.Xaml.XamlObjectWriter.WriteStartMember(XamlMember 属性) 在 System.Xaml.XamlWriter.WriteNode(XamlReader reader) 在 System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader,XamlObjectWriter currentWriter)

在此先感谢您的帮助

double N = (double)values[2];

当 MultiBinding 中的第三个绑定绑定到整数时是无效的转换:

<Binding ... Path="Items.Count"/>

因此转换为整数

var N = (int)values[2];

您还应该确保转换器总是 returns 一个双倍的,所以替换

return 300;

return 300d;