【发布时间】:2016-04-20 10:03:16
【问题描述】:
我不明白为什么当用户双击列表视图项时我的命令没有正确执行。我知道事实上错误发生在它的绑定方式上。我不确定如何正确绑定到 Windows 数据上下文。
如何从嵌套控件绑定到 Windows 数据上下文。
这里是有问题的代码被隔离...
<Grid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding EditCustomerCommand}"/>
</Grid.InputBindings>
MainWindow.xaml
<Window.DataContext>
<viewModel:MainWindowViewModel />
</Window.DataContext>
<!--<Grid>
<ContentPresenter Content="{Binding ActiveViewModel}"></ContentPresenter>
</Grid>-->
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="Add New Person" Command="{Binding AddNewPersonCommand}"/>
<ListView Grid.Row="1" ItemsSource="{Binding People}">
<ListView.View>
<GridView>
<GridViewColumn Header="First Name" Width="150">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid Margin="0">
<StackPanel Orientation="Horizontal">
<Button Content="X" Width="20"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.RemovePersonCommand}"
CommandParameter="{Binding DataContext, RelativeSource={RelativeSource Self}}"
/>
<Label Content="{Binding FirstName}"/>
</StackPanel>
<Grid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding EditCustomerCommand}"/>
</Grid.InputBindings>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
</Style>
</ListView.Resources>
<ListView.InputBindings>
<KeyBinding Key="Delete" Command="{Binding DeleteCustomersCommand}"/>
</ListView.InputBindings>
</ListView>
<StackPanel Grid.Row="2" Orientation="Horizontal">
<Label Content="# People"/>
<Label Content="{Binding PersonCount}"/>
</StackPanel>
</Grid>
</Window>
【问题讨论】:
-
列表视图正在吞噬鼠标事件
-
@thumbmunkeys 你有什么建议吗?
-
使用 ItemsControl 而不是 ListView 应该可以工作:stackoverflow.com/questions/3356719/…
-
什么?!不,那根本行不通。我将不得不进入并设置所有选择命令,而不是什么。这是不正确的。