WPF:将我的CheckBox绑定到常用的纯XAML

时间:2019-02-09 06:08:12

标签: wpf checkbox

我尝试将CheckBox绑定到commnd中。

基本视图模型

public ViewModelBase()
{
    SelectedFileCommand = new SelectedFileCommand(this);
}

<Page.DataContext>
        <viewmodel:ViewModelBase/>
</Page.DataContext>

命令

public class SelectedFileCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;
        public ViewModelBase ViewModel { get; set; }

        public SelectedFileCommand(ViewModelBase viewModel)
        {
            ViewModel = viewModel;
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {

        }
    }
}

我的复选框

<CheckBox IsChecked="{Binding IsSelected}"              
           <i:Interaction.Triggers>
                 <i:EventTrigger EventName="Checked">
                        <i:InvokeCommandAction Command="{Binding SelectedFileCommand}" CommandParameter="{Binding}"/>
                 </i:EventTrigger>
           </i:Interaction.Triggers>
</CheckBox>

我也尝试:

<CheckBox DataContext="{Binding}"                                                      
          <i:Interaction.Triggers>
               <i:EventTrigger EventName="IsChecked">
                       <i:InvokeCommandAction Command="{Binding SelectedFileCommand}"
                                              CommandParameter="CheckBox.IsChecked"/>
                </i:EventTrigger>
         </i:Interaction.Triggers>

但是我的Execute函数没有被调用。

编辑

我忘了说这个CheckBoxListViewItem里面

工作解决方案

<CheckBox IsChecked="{Binding IsSelected}"
          Command="{Binding DataContext.CheckBoxSelectedFileCommand, ElementName=mainView}"
          CommandParameter="{Binding IsChecked}"/>

1 个答案:

答案 0 :(得分:0)

如果当您说Command =“ {Binding SelectedFileCommand}”时复选框处于列表视图中,则将绑定到列表视图项的数据上下文。如果您的命令在窗口的视图模型中,则此命令将无效。这样的东西将绑定到主视图模型中的命令。

Command="{Binding DataContext.SelectedFileCommand, ElementName=mainView}"

在这里,我给窗口x:Name = mainView。这样,我可以绑定到其dataContext的属性。

而且,IsChecked不是您应该使用“已检查”的事件。

最后,命令参数问题。由于复选框有两个不同的事件(“已选中” /“未选中”),因此您可以使用两个命令而不传递任何参数。或者,您可以在列表项viewmodel中放置一个属性,例如;

public bool IsChecked { get; set; }

,然后可以将复选框的IsChecked属性绑定到此属性。最后,您可以将命令参数绑定到该新属性。

编辑:完整示例

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        x:Name="mainView"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ListView ItemsSource="{Binding Items}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <CheckBox Content="{Binding Text}" IsChecked="{Binding IsChecked}">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Checked">
                                <i:InvokeCommandAction Command="{Binding DataContext.SelectedFileCommand, ElementName=mainView}"
                                              CommandParameter="{Binding IsChecked}"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </CheckBox>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>

隐藏代码:

using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;

namespace WpfApp2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainViewModel();
        }
    }

    public class MainViewModel
    {
        public ObservableCollection<ItemViewModel> Items { get; set; } = new ObservableCollection<ItemViewModel>();
        public ICommand SelectedFileCommand { get; set; }
        public MainViewModel()
        {
            SelectedFileCommand = new SelectedFileCommand(this);
            this.Items.Add(new ItemViewModel() { Text = "Item 1" });
            this.Items.Add(new ItemViewModel() { Text = "Item 2" });
            this.Items.Add(new ItemViewModel() { Text = "Item 3" });
            this.Items.Add(new ItemViewModel() { Text = "Item 4" });

        }
    }

    public class ItemViewModel
    {
        public string Text { get; set; }

        public bool IsChecked { get; set; }
    }

    public class SelectedFileCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;
        public MainViewModel ViewModel { get; set; }

        public SelectedFileCommand(MainViewModel viewModel)
        {
            ViewModel = viewModel;
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            var x = parameter;
        }
    }
}