Silverlight 绑定 - CommandParameter 到父级(帮助删除 x:Name)
知道如何绑定到列表框而不使用 x:Name 作为下面的代码吗?我在 ElementName=myList 处使用 xname
<ListBox x:Name="myList" Grid.Row="1" Height="auto" ItemsSource="{Binding Path=ListItems}" ItemContainerStyle="{StaticResource StretchedItemContainerStyle}" ScrollViewer.VerticalScrollBarVisibility="Disabled" Background="Transparent">
<wi:Interaction.Triggers>
<wi:EventTrigger EventName="SelectionChanged">
<wi:InvokeCommandAction Command="{Binding Source={StaticResource Locator}, Path=ViewModel.Command}" CommandParameter="{Binding SelectedItem, ElementName=myList}" />
</wi:EventTrigger>
</wi:Interaction.Triggers>
<ListBox.ItemTemplate>
我真的不想每次使用 InvokeCommandAction 时都必须为我的控件定义名称。注意:wi 是 SL4/WP7.1 的 Windows 交互性
谢谢!
Any idea how I can bind to the listbox and not use an x:Name for the code below? I'm using the xname at ElementName=myList
<ListBox x:Name="myList" Grid.Row="1" Height="auto" ItemsSource="{Binding Path=ListItems}" ItemContainerStyle="{StaticResource StretchedItemContainerStyle}" ScrollViewer.VerticalScrollBarVisibility="Disabled" Background="Transparent">
<wi:Interaction.Triggers>
<wi:EventTrigger EventName="SelectionChanged">
<wi:InvokeCommandAction Command="{Binding Source={StaticResource Locator}, Path=ViewModel.Command}" CommandParameter="{Binding SelectedItem, ElementName=myList}" />
</wi:EventTrigger>
</wi:Interaction.Triggers>
<ListBox.ItemTemplate>
I really don't want to have to define a name for my control everytime I use the InvokeCommandAction. Note: wi is Windows Interactivity for SL4/WP7.1
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试绑定到“SelectedItem”,并完全消除传递 CommandParameter 的需要。不要忘记Mode=TwoWay。在 ViewModel 中添加“SelectedListItem”INotifyPropertyChanged getter/setter 并绑定到该属性。
Try binding to the "SelectedItem" instead and remove the need to pass a CommandParameter altogether. Don't forget the Mode=TwoWay. Add a "SelectedListItem" INotifyPropertyChanged getter/setter in you ViewModel and bind to that property.
我不确定这是否真的可能,所以当我怀疑并等待其他人回答时,我会推荐一种不同的方法。
您可以使用附加行为来执行此操作,这是一种简单而简洁的方法,可以提供更干净的代码。
我已经写过关于它的我的博客,并为其发布了一个框架,那就是 可在 NuGet 上使用。
ListBox
需要的是SelectorExtension
。如果您只想要代码,请不要再犹豫了:SelectorExtension.cs。 (来源已获得 MIT 许可)这意味着您只需将代码更改为以下内容:
其中
ext
为xmlns:ext="clr-namespace:ToolkitExtensions;clr- assembly=ToolkitExtensions"< /代码>
I'm not sure if that's actually possible, so while I doubt, and wait for others to answer, I would recommend a different approach.
You can use attached behaviours to do this, which is a simple and neat approach, giving a lot more clean code.
I've written about it on my blog, and published a framework for it, that's available on NuGet.
What you need for your
ListBox
is theSelectorExtension
. If you just want the code, look no futher than here: SelectorExtension.cs. (The source is MIT licensed)This means you can simply change your code to the following:
where
ext
beingxmlns:ext="clr-namespace:ToolkitExtensions;clr-assembly=ToolkitExtensions"