为什么列表框中选定的项目会在表达式混合中引发错误?
我正在使用以下链接学习表达混合 http://visitmix.com/labs/rosetta/EyesOfBlend /数据模板/ 这是关于使用表达式混合的非常好的分步说明。我能够成功完成所有步骤,并且能够运行该程序并得到最终结果。
在步骤(9)之后,在运行项目之前,当我检查设计器时,设计器没有在我在步骤(9)中添加的大图像控件中显示图像。我知道为什么了,由于ListBox的selected索引是-1,所以我将selectedindex更改为0,现在我能够看到图像了。但是,当我编译代码时,出现以下错误
“指定的参数超出了有效值的范围。参数名称:SelectedIndex”
XAML 是
<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource SampleDataSource}}">
<Grid x:Name="itemGrid" DataContext="{Binding SelectedItem, ElementName=listBox}">
<Image Margin="185,56,153,160" Stretch="Fill" Source="{Binding Name}"/>
</Grid>
<ListBox x:Name="listBox" SelectedIndex="0" Margin="8,0,0,8" ItemsSource="{Binding Collection}" ItemTemplate="{StaticResource ItemTemplate1}" ItemsPanel="{StaticResource ItemsPanelTemplate1}" Height="101" VerticalAlignment="Bottom"/>
</Grid>
如果我删除选定的索引,则所有编译都会良好。有人可以解释为什么它在编译后第一次显示,它停止工作吗?
谢谢
I am learning expression blend using the following link http://visitmix.com/labs/rosetta/EyesOfBlend/DataTemplates/
It is a very good step by step instruction on using expression blend. I was able to do all the steps successfully and I was able to run the program and got the final result.
Right after step (9), before running the project, when I checked the designer, the designer did not show the image in the big image control I have added in step (9). I knew why, since the selected index of the ListBox was -1, so I changed the selectedindex to 0, now I was able to see the image. But when I compiled the code I get the following error
"Specified argument was out of the range of valid values. Parameter name:SelectedIndex"
XAML is
<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource SampleDataSource}}">
<Grid x:Name="itemGrid" DataContext="{Binding SelectedItem, ElementName=listBox}">
<Image Margin="185,56,153,160" Stretch="Fill" Source="{Binding Name}"/>
</Grid>
<ListBox x:Name="listBox" SelectedIndex="0" Margin="8,0,0,8" ItemsSource="{Binding Collection}" ItemTemplate="{StaticResource ItemTemplate1}" ItemsPanel="{StaticResource ItemsPanelTemplate1}" Height="101" VerticalAlignment="Bottom"/>
</Grid>
If I remove the selected index, all compile good. Could someone explain why it showed first time after compiling, it stopped working?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
绑定到 SelectedItem 几乎总是比绑定到 SelectedIndex 更好。如果 Item 为 null,则绑定应该正常失败。
You are almost always better binding to the SelectedItem than the SelectedIndex. If the Item is null, the binding should fail gracefully.
这可能是因为“Collection”属性在列表框呈现后绑定到项目源。因此,如果没有集合(空),则第一个索引 [0] 不存在,并且会抛出“超出范围异常”。
要解决您的问题,请在填充集合后在代码隐藏中设置选定的索引。希望有帮助。
This is probably because the "Collection" attribute gets bound to the itemsource after the listbox is rendered. So if there is no collection(empty) then the first index [0] does not exist and it throws an "out of range exception".
To resolve your issue set the selected index in code-behind after collection is populated. Hope that helps.