ListView 上的按钮 - 使用 MVVM

发布于 2024-10-19 10:44:54 字数 1726 浏览 3 评论 0原文

我在列表视图控件上有一个按钮。我已将此控件绑定到 ViewModel 类的基类上的命令之一。如果我在列表视图之外放置一个按钮,它可以使用相同的命令正常工作。但是当我将其放在列表视图上时,该命令不会被触发。

你能想到一个理由吗???

下面是片段:

<ListView Grid.Row="2" AlternationCount="2" ItemsSource="{Binding Path=AObject}" Margin="20" MaxHeight="200">
            <ListView.DataContext>
                <local:MyViewModel/>
            </ListView.DataContext>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Run ID" DisplayMemberBinding="{Binding Path=RID}" />
                    <GridViewColumn Header="Job ID" DisplayMemberBinding="{Binding Path=JID}" />
                    <GridViewColumn Header="Run Description">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ContentPresenter Content="{Binding Path=OpenScCommand}" HorizontalAlignment="Right"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Edit">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button  Command="{Binding ShowItemCommand}" CommandParameter="{Binding Path=RID}" Content="_Edit email run" IsDefault="False"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

I have a button on a listview control. I have bound this control to one of the command on the base class of the ViewModel class. If I place a button outside of the listview it works fine with the same command. But the command does not get fired when I place it on the listview.

can you think of a reason????

Below is the snippet:

<ListView Grid.Row="2" AlternationCount="2" ItemsSource="{Binding Path=AObject}" Margin="20" MaxHeight="200">
            <ListView.DataContext>
                <local:MyViewModel/>
            </ListView.DataContext>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Run ID" DisplayMemberBinding="{Binding Path=RID}" />
                    <GridViewColumn Header="Job ID" DisplayMemberBinding="{Binding Path=JID}" />
                    <GridViewColumn Header="Run Description">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ContentPresenter Content="{Binding Path=OpenScCommand}" HorizontalAlignment="Right"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Edit">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button  Command="{Binding ShowItemCommand}" CommandParameter="{Binding Path=RID}" Content="_Edit email run" IsDefault="False"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

森末i 2024-10-26 10:44:54

这是因为该按钮位于 ListViewItem 中,因此它继承了包含它的项目的 DataContext。以下是绑定到 ListView 本身的 DataContext 的方法:

<Button  Command="{Binding ShowItemCommand}" DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}" ...

附带说明:根据命令的作用,最好将其放入项目的 ViewModel 中

That's because the button is in the ListViewItem, so it inherits the DataContext of the item that contains it. Here's how you can bind to the DataContext of the ListView itself:

<Button  Command="{Binding ShowItemCommand}" DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}" ...

As a side note: depending on what the command does, it might be better to put it in the ViewModel of the items

成熟的代价 2024-10-26 10:44:54

当您将按钮放入列表视图中时,它会获取一个新的 DataContext - 它会获取列表中当前项目的 DataContext,因此会丢失原始的 DataContext。

最佳解决方案是使用 ViewModelLocator

When you put the button inside the listview it gets a new DataContext - it gets the DataContext of the current item in the list, and therefor it loses the original DataContext.

The best solution to this is using ViewModelLocator

所有深爱都是秘密 2024-10-26 10:44:54

您也可以用更少的代码来完成此操作。

<Button Command="{Binding ShowItemCommand}">
    <Button.DataContext>
        <local:MyViewModel/>
    </Button.DataContext>
 </Button>

You can also do this with a little less code..

<Button Command="{Binding ShowItemCommand}">
    <Button.DataContext>
        <local:MyViewModel/>
    </Button.DataContext>
 </Button>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文