所以我有这个ListView
和我的对象ItemSource
和ListViewItem
:
<GridViewColumn Header="Select" Width="45">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Command="{Binding SelectedInterfaceCommand}"
CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}">
</CheckBox>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
ViewModelBase
public ViewModelBase()
{
SelectInterfaceCommand = new SelectedInterfaceCommand(this);
}
然后我尝试在CheckBox
IsChecked
内捕获SelectedInterfaceCommand
Command
事件:
public class SelectedInterfaceCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public ViewModelBase ViewModel { get; set; }
public SelectedInterfaceCommand(ViewModelBase viewModel)
{
ViewModel = viewModel;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
}
}
我的CanExecute
和Execute
从未打电话。
我也尝试这种方法:
在我的Object类中,我有这个Property
:
public bool IsSelected
{
get { return isSelected; }
set
{
isSelected = value;
OnStaticPropertyChanged();
}
}
XAML
<GridViewColumn Header="Select" Width="45">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox
<CheckBox.IsChecked>
<Binding Path="IsSelected" RelativeSource="{RelativeSource AncestorType={x:Type ListViewItem}}"/>
</CheckBox.IsChecked>
</CheckBox>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
再一次,这个IsSelected
的设置员从未调用过。