如何在项目控件中绑定复选框的 IsChecked 属性



我有以下 ItemsControl,它为可用集合中的每个数据库提供了一个复选框。 这些复选框允许用户选择要筛选的复选框。 要筛选的数据库位于单独的集合(筛选的数据库)中。 我究竟该怎么做? 我可以将 InFilter 属性添加到数据库项类中。 但是,我还不想开始更改此代码。 我无法解决的问题是我需要绑定到不在数据库项本身上的属性。 有什么想法吗?

<ItemsControl ItemsSource="{Binding AvailableDatabases}">
   <ItemsControl.ItemTemplate>
      <DataTemplate>
         <CheckBox Content="{Binding Name}" IsChecked="{Binding ???}"/>
      </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>
// In view model
public IBindingList FilteredDatabases
{
  get;
  private set;  
}
public IBindingList AvailableDatabases
{
   get;
   private set;
}
  1. 将复选框命令绑定到路由命令实例
  2. 将路由命令绑定到方法
  3. 使用 IBindingList.Add
  4. 和 IBindingList.Remove 方法

下面的代码说明了你正在尝试做什么,为了做到这一点,你最好使用 ObservableCollection 而不是作为你的集合对象,如果 ItemsControl 绑定到它,它将在添加和删除视图模型时自动更新 UI。

XAML:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <ItemsControl Grid.Column="0" ItemsSource="{Binding AvailableDatabases}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    <ItemsControl Grid.Column="1" ItemsSource="{Binding FilteredDatabases}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

查看模型:

public class MainViewModel
{
    private ObservableCollection<DBViewModel> _availableDatabases;
    private ObservableCollection<DBViewModel> _filteredDatabases;
    public ObservableCollection<DBViewModel> AvailableDatabases
    {
        get
        {
            if (_availableDatabases == null)
            {
                _availableDatabases = new ObservableCollection<DBViewModel>(new List<DBViewModel>()
                    {
                        new DBViewModel(this) { Name = "DB1" , IsChecked = true},
                        new DBViewModel(this) { Name = "DB2" },
                        new DBViewModel(this) { Name = "DB3" },
                        new DBViewModel(this) { Name = "DB4" },
                        new DBViewModel(this) { Name = "DB5" },
                        new DBViewModel(this) { Name = "DB6" },
                        new DBViewModel(this) { Name = "DB7" , IsChecked = true },
                    });

            }
            return this._availableDatabases;
        }
    }
    public ObservableCollection<DBViewModel> FilteredDatabases
    {
        get
        {
            if (_filteredDatabases == null)
                _filteredDatabases = new ObservableCollection<DBViewModel>(new List<DBViewModel>());
            return this._filteredDatabases;
        }
    }
}
public class DBViewModel
{
    private MainViewModel _parentVM;
    private bool _isChecked;
    public string Name { get; set; }
    public DBViewModel(MainViewModel _parentVM)
    {
        this._parentVM = _parentVM;
    }
    public bool IsChecked
    {
        get
        {
            return this._isChecked;
        }
        set
        {
            //This is called when checkbox state is changed
            this._isChecked = value;
            //Add or remove from collection on parent VM, perform sorting here
            if (this.IsChecked)
                _parentVM.FilteredDatabases.Add(this);
            else
                _parentVM.FilteredDatabases.Remove(this);
        }
    }
}

视图模型也应该实现 INotifyPropertyChanged,我省略了它,因为在这种特殊情况下没有必要。

最新更新