ListView 上的按钮 - 使用 MVVM
我在列表视图控件上有一个按钮。我已将此控件绑定到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为该按钮位于
ListViewItem
中,因此它继承了包含它的项目的 DataContext。以下是绑定到ListView
本身的 DataContext 的方法:附带说明:根据命令的作用,最好将其放入项目的 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 theListView
itself:As a side note: depending on what the command does, it might be better to put it in the ViewModel of the items
当您将按钮放入列表视图中时,它会获取一个新的 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
您也可以用更少的代码来完成此操作。
You can also do this with a little less code..