Silverlight 绑定 - CommandParameter 到父级(帮助删除 x:Name)

发布于 2024-11-29 08:09:58 字数 817 浏览 1 评论 0原文

知道如何绑定到列表框而不使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离笑几人歌 2024-12-06 08:09:58

尝试绑定到“SelectedItem”,并完全消除传递 CommandParameter 的需要。不要忘记Mode=TwoWay。在 ViewModel 中添加“SelectedListItem”INotifyPropertyChanged getter/setter 并绑定到该属性。

<ListBox SelectedItem={Binding SelectedListItem, Mode=TwoWay} 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}" />
        </wi:EventTrigger>
    </wi:Interaction.Triggers>
    <ListBox.ItemTemplate>
</ListBox>

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.

<ListBox SelectedItem={Binding SelectedListItem, Mode=TwoWay} 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}" />
        </wi:EventTrigger>
    </wi:Interaction.Triggers>
    <ListBox.ItemTemplate>
</ListBox>
苏佲洛 2024-12-06 08:09:58

我不确定这是否真的可能,所以当我怀疑并等待其他人回答时,我会推荐一种不同的方法。

您可以使用附加行为来执行此操作,这是一种简单而简洁的方法,可以提供更干净的代码。

我已经写过关于它我的博客,并为其发布了一个框架,那就是 可在 NuGet 上使用

ListBox 需要的是 SelectorExtension。如果您只想要代码,请不要再犹豫了:SelectorExtension.cs。 (来源已获得 MIT 许可)

这意味着您只需将代码更改为以下内容:

<ListBox ext:SelectorExtension.Command="{Binding Source={StaticResource Locator}, Path=ViewModel.Command}"
         Grid.Row="1"
         Height="Auto"
         ItemsSource="{Binding Path=ListItems}"
         ItemContainerStyle="{StaticResource StretchedItemContainerStyle}"
         ScrollViewer.VerticalScrollBarVisibility="Disabled"
         Background="Transparent">
    ...

其中 extxmlns: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 the SelectorExtension. 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:

<ListBox ext:SelectorExtension.Command="{Binding Source={StaticResource Locator}, Path=ViewModel.Command}"
         Grid.Row="1"
         Height="Auto"
         ItemsSource="{Binding Path=ListItems}"
         ItemContainerStyle="{StaticResource StretchedItemContainerStyle}"
         ScrollViewer.VerticalScrollBarVisibility="Disabled"
         Background="Transparent">
    ...

where ext being xmlns:ext="clr-namespace:ToolkitExtensions;clr-assembly=ToolkitExtensions"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文