WrapPanel ItemWidth & ItemHeight MultiBinding



我只是想用MultiBinding代替WrapPanelItemHeight &ItemWidth。代码如下:

<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,因此,当我在线阅读时,我必须在ListView中使用WrapPanel(如上所述)。在此之前,我手动填充WrapPanel,我有一个方法,我用来计算WrapPanelItemHeightItemWidth并分配给它。但是现在WrapPanelListView内部,它在代码后面是不可访问的,因此我决定使用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:指定的类型转换无效。

堆栈跟踪:在AvaPa.SensorHeightCalculator

。转换(对象[]值,类型targetType,对象参数,CultureInfo文化)在System.Windows.Data.MultiBindingExpression.TransferValue ()在System.Windows.Data.MultiBindingExpression.Transfer ()在System.Windows.Data.MultiBindingExpression。UpdateTarget(布尔includeInnerBindings)在System.Windows.Data.MultiBindingExpression。AttachToContext(布尔lastChance)在System.Windows.Data.MultiBindingExpression。AttachOverride(DependencyObject d, DependencyProperty dp)在System.Windows.Data.BindingExpressionBase。OnAttach(DependencyObject d, DependencyProperty dp)在System.Windows.StyleHelper。GetInstanceValue(UncommonField 1 dataField, DependencyObject container, FrameworkElement feChild, FrameworkContentElement fceChild, Int32 childIndex, DependencyProperty dp, Int32 i, EffectiveValueEntry& entry) at System.Windows.StyleHelper.GetChildValueHelper(UncommonField 1 dataField, ItemStructList 1& valueLookupList, DependencyProperty dp, DependencyObject container, FrameworkObject child, Int32 childIndex, Boolean styleLookup, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot) at System.Windows.StyleHelper.GetChildValue(UncommonField 1 dataField, DependencyObject容器,Int32 childIndex, FrameworkObject子,DependencyProperty dp, FrugalStructList 1& childRecordFromChildIndex, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot) at System.Windows.StyleHelper.GetValueFromTemplatedParent(DependencyObject container, Int32 childIndex, FrameworkObject child, DependencyProperty dp, FrugalStructList 1&childRecordFromChildIndex, frameelementfactory templateRoot, effecvevalueentry &条目)在System.Windows.StyleHelper。ApplyTemplatedParentValue(DependencyObject容器,FrameworkObject子,Int32 childIndex, FrugalStructList 1& childRecordFromChildIndex, DependencyProperty dp, FrameworkElementFactory templateRoot) at System.Windows.StyleHelper.InvalidatePropertiesOnTemplateNode(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList)childRecordFromChildIndex, Boolean isDetach, FrameworkElementFactory, templateRoot)在System.Windows.FrameworkTemplate。InvalidatePropertiesOnTemplate(DependencyObject容器,对象currentObject)在System.Windows.FrameworkTemplate。HandleBeforeProperties(对象createdObject, DependencyObject&rootObject, DependencyObject容器,FrameworkElement容器,INameScope在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读者)在System.Windows.FrameworkTemplate。LoadTemplateXaml(XamlReader templateReader, xamlobjectwwriter当前twwriter)

提前感谢您的帮助

double N = (double)values[2];
当MultiBinding中的第三个绑定绑定到一个整数时,

是一个无效的强制转换:

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

转换为整型

var N = (int)values[2];

您还应该确保转换器总是返回double类型,因此替换

return 300;

return 300d;

相关内容

  • 没有找到相关文章

最新更新