我只想将MultiBinding
用于WrapPanel
的{{1}}& ItemHeight
。代码是这样的:
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
内的WrapPanel
1}}(如上所述)。在此之前,我手动填写ListView
我有一个方法,我曾经用它来计算WrapPanel
的{{1}}和ItemHeight
并分配给它。但是现在ItemWidth
位于WrapPanel
内,但在代码隐藏中无法访问,因此我决定使用WrapPanel
。
SensorHeightCalculator的来源:
ListView
异常和完整堆栈跟踪:
异常:InvalidCastException:指定的强制转换无效。
堆栈追踪:
at AvaPa.SensorHeightCalculator.Convert(Object [] values,Type targetType,Object parameter,CultureInfo culture)
在System.Windows.Data.MultiBindingExpression.TransferValue()
在System.Windows.Data.MultiBindingExpression.Transfer()
在System.Windows.Data.MultiBindingExpression.UpdateTarget(Boolean includeInnerBindings)
在System.Windows.Data.MultiBindingExpression.AttachToContext(Boolean lastChance)
在System.Windows.Data.MultiBindingExpression.AttachOverride(DependencyObject d,DependencyProperty dp)
在System.Windows.Data.BindingExpressionBase.OnAttach(DependencyObject d,DependencyProperty dp)
在System.Windows.StyleHelper.GetInstanceValue(UncommonField Multibinding
1 dataField,ItemStructList 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[]);
}
}
1 dataField,DependencyObject容器,Int32 childIndex,FrameworkObject子,DependencyProperty dp,FrugalStructList 1 dataField, DependencyObject container, FrameworkElement feChild, FrameworkContentElement fceChild, Int32 childIndex, DependencyProperty dp, Int32 i, EffectiveValueEntry& entry)
at System.Windows.StyleHelper.GetChildValueHelper(UncommonField
1&amp; childRecordFromChildIndex,FrameworkElementFactory templateRoot,EffectiveValueEntry&amp; entry)
在System.Windows.StyleHelper.ApplyTemplatedParentValue(DependencyObject容器,FrameworkObject子,Int32 childIndex,FrugalStructList 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&amp; childRecordFromChildIndex,Boolean isDetach,FrameworkElementFactory templateRoot)
在System.Windows.FrameworkTemplate.InvalidatePropertiesOnTemplate(DependencyObject容器,Object currentObject)
在System.Windows.FrameworkTemplate.HandleBeforeProperties(Object createdObject,DependencyObject&amp; rootObject,DependencyObject容器,FrameworkElement feContainer,INameScope nameScope)
在System.Windows.FrameworkTemplate。&lt;&gt; 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)
提前感谢您的帮助
答案 0 :(得分:3)
double N = (double)values[2];
当MultiBinding中的第三个绑定绑定到整数时,是无效的转换:
<Binding ... Path="Items.Count"/>
所以强制转换为整数
var N = (int)values[2];
您还应确保转换器始终返回double,因此请替换
return 300;
与
return 300d;