[英]Windows 8.1 to 10 XAML - the parameter is incorrect exception
I have a Windows 8.1 app that runs fine in Windows 8.1, but when running on a Windows 10 machine, I encounter a XAML exception, with the unhandled exception message, "The parameter is incorrect." 我有一个Windows 8.1应用程序,可以在Windows 8.1中正常运行,但是在Windows 10计算机上运行时,遇到XAML异常,并带有未处理的异常消息,“参数不正确”。
When I change the debugger type to mixed (managed and native), the exception message is "Value does not fall within the expected range." 当我将调试器类型更改为混合(托管和本机)类型时,异常消息为“值未在预期范围内”。
Original XAML 原始XAML
<ListView x:Name="myListView"
Grid.Row="3"
Margin="10"
HorizontalAlignment="Center"
VerticalAlignment="Top"
BorderBrush="Gray"
BorderThickness="1"
FontSize="{StaticResource FontSizeMedium}"
ItemsSource="{Binding LookupList}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Width="{Binding DataContext.ColumnWidth, ElementName=myListView}" Visibility="{Binding IsVisible, Converter={StaticResource BooleanToVisibilityConverter}}">
<ListView Name="gridList"
Grid.Row="1"
VerticalAlignment="Top"
FontSize="{StaticResource FontSizeMedium}"
ItemsSource="{Binding ColumnValues}"
SelectedIndex="{Binding DataContext.SelectedIndex,
Mode=TwoWay,
ElementName=myListView}"
SelectionMode="Single">
</ListView>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
If I remove the ElementName in the SelectedIndex binding, the list view loads (but does not have the correct functionality). 如果我在SelectedIndex绑定中删除ElementName,则将加载列表视图(但功能不正确)。
Modified XAML (loads without exception, but not the correct functionality) 修改的XAML(无例外加载,但功能不正确)
<ListView x:Name="myListView"
Grid.Row="3"
Margin="10"
HorizontalAlignment="Center"
VerticalAlignment="Top"
BorderBrush="Gray"
BorderThickness="1"
FontSize="{StaticResource FontSizeMedium}"
ItemsSource="{Binding LookupList}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Width="{Binding DataContext.ColumnWidth, ElementName=myListView}" Visibility="{Binding IsVisible, Converter={StaticResource BooleanToVisibilityConverter}}">
<ListView Name="gridList"
Grid.Row="1"
VerticalAlignment="Top"
FontSize="{StaticResource FontSizeMedium}"
ItemsSource="{Binding ColumnValues}"
SelectedIndex="{Binding DataContext.SelectedIndex,
Mode=TwoWay}"
SelectionMode="Single">
</ListView>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Is ElementName binding in this manner still supported, or are there additional debugging steps that can yield more information? 是否仍然支持以这种方式绑定ElementName,还是有其他调试步骤可以产生更多信息?
I am surprised that it's even working in Windows Phone 8.1 because the binding is incorrect. 我很惊讶它甚至可以在Windows Phone 8.1中运行,因为绑定不正确。
This binding expression {Binding DataContext.SelectedIndex, Mode=TwoWay, ElementName=myListView}
here basically means to go locate a control named myListView
and then find a property called SelectedIndex
of the control's DataContext
(which is in most case, the ViewModel ). 此绑定表达式
{Binding DataContext.SelectedIndex, Mode=TwoWay, ElementName=myListView}
在这里基本上意味着要定位名为myListView
的控件,然后找到该控件的DataContext
名为SelectedIndex
的属性(在大多数情况下为ViewModel )。
But really the SelectedIndex
should be the dependency property of the ListView
control . 但是,实际上
SelectedIndex
应该是ListView
控件的依赖项属性 。
So the fix is simple - just remove the DataContext.
因此修复很简单-只需删除
DataContext.
- -
SelectedIndex="{Binding SelectedIndex, Mode=TwoWay, ElementName=myListView}"
To access the items container for your itemtemplate, you have to use RelativeSource to find the ancestor that contains your item template. 要访问您的itemtemplate的项目容器,您必须使用RelativeSource查找包含您的项目模板的祖先。
In your example, it would mean defining this kind of binding 在您的示例中,这意味着要定义这种绑定
SelectedIndex="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Path=SelectedIndex,Mode=TwoWay}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.