WPF:将我的Checkbox IsChecked事件绑定到我的模型

时间:2019-02-06 21:12:19

标签: wpf checkbox binding

所以我有这个ListView和我的对象ItemSourceListViewItem

<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)
        {

        }
    }

我的CanExecuteExecute从未打电话。

我也尝试这种方法:

在我的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的设置员从未调用过。

0 个答案:

没有答案