【发布时间】:2016-06-25 21:39:05
【问题描述】:
我只是想将MultiBinding 用于WrapPanel 的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,因此,当我在网上阅读时,我必须在ListView 中使用WrapPanel(如上)。在此之前,我手动填写了WrapPanel,我有一个方法用于计算WrapPanel 的ItemHeight 和ItemWidth 并分配给它。但是现在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:指定的转换无效。
堆栈跟踪:
在 AvaPa.SensorHeightCalculator.Convert(Object[] values, Type targetType, Object parameter, CultureInfoculture)
在 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 容器,Int32 childIndex,FrameworkObject 子项,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、Boolean 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 参数)
在 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, XamlObjectWriter currentWriter)
提前感谢您的帮助
【问题讨论】:
-
“但它会抛出异常”——什么异常?推荐阅读-meta.stackoverflow.com/questions/260648/….
-
这是例外,但我认为并没有真正的帮助:InvalidCastException: Specified cast is not valid.
-
请阅读meta.stackoverflow.com/questions/260648/…,然后将该异常的完整堆栈跟踪添加到您的问题中(不是评论)。另外,这个异常可能发生在
local:SensorHeightCalculator,所以也应该添加它的源代码。 -
@EugenePodskal 我很确定该异常与 SensorHeightCalculator 无关,但如果您这么认为,我补充一下
-
那个堆栈跟踪似乎不同意你的看法 -
at AvaPa.SensorHeightCalculator.Convert
标签: c# wpf xaml multibinding wrappanel