简体   繁体   English

如何在ItemsControl中绑定CheckBox的IsChecked属性?

[英]How do I bind IsChecked property of CheckBox within an ItemsControl?

I've got the following ItemsControl that gives me a check box for every database within the available collection. 我有以下ItemsControl,它为可用集合中的每个数据库提供了一个复选框。 These checkboxes allow the user to select which ones to filter on. 这些复选框允许用户选择要过滤的复选框。 The databases to filter on are in a separate collection (FilteredDatabases). 要过滤的数据库位于单独的集合(FilteredDatabases)中。 How exactly do I do this? 我到底该怎么做? I could add an InFilter property to the database item class. 我可以将InFilter属性添加到数据库项类。 But, I don't want to start changing this code yet. 但是,我还不想开始更改此代码。 The problem I can't get around in my head is the fact that I need to bind to a property that is not on the database item itself. 我无法解决的问题是我需要绑定到不在数据库项目本身上的属性。 Any ideas? 有任何想法吗?

<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. Bind CheckBox.Command to routed command instance 将CheckBox.Command绑定到路由命令实例
  2. Bind routed command to method 将路由命令绑定到方法
  3. Use IBindingList.Add and IBindingList.Remove methods 使用IBindingList.Add和IBindingList.Remove方法

The following code illustrates what you are trying to do, in order to do this you are better off using ObservableCollection instead of as your collection object, if an ItemsControl is bound to it it will automatically update the UI when viewmodels are added and removed. 下面的代码说明了您要尝试执行的操作,为此,最好使用ObservableCollection而不是将其用作集合对象,如果将ItemsControl绑定到它,它将在添加和删除视图模型时自动更新UI。

XAML: 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>

View Models: 查看模型:

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

        }
    }
}

View models should also implement INotifyPropertyChanged, I omitted it since it was not necessary in this particular case. 视图模型还应该实现INotifyPropertyChanged,我省略了它,因为在这种情况下没有必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将另一个DependencyProperty绑定到CheckBox的IsChecked属性? - How can I bind another DependencyProperty to the IsChecked Property of a CheckBox? 如何为我的bool属性正确实现INotifyPropertyChanged并绑定到CheckBox.IsChecked? - How do I correctly implement INotifyPropertyChanged for my bool property and bind to CheckBox.IsChecked? 在WPF中,如何将Checkbox的IsChecked绑定到List &lt;&gt;。Contains? - In WPF, how do I two-way bind a Checkbox's IsChecked to property to List<>.Contains? 如何将两个东西绑定到复选框的 IsChecked 属性? - How to bind two things to IsChecked property of checkbox? 如何将 WPF 复选框的 IsChecked 属性绑定到非窗口对象的布尔属性 - How can I bind a WPF checkbox's IsChecked property to a boolean property of an object that is not a window 如何将对象的布尔属性绑定到CheckBox的IsChecked属性? - How to bind an object's boolean property to a CheckBox's IsChecked property? 如何将TextBox的TextWrapping属性绑定到MenuItem的IsChecked值? - How do you bind the TextWrapping property of a TextBox to the IsChecked value of a MenuItem? 如何在 WPF 的 ItemsControl 中绑定 2 个列表? - How do I bind 2 Lists in ItemsControl in WPF? 如何绑定列表 <string> 到ItemsControl? - How do I bind a List<string> to an ItemsControl? 如何将CheckBox IsChecked绑定到CheckBoxes的ListBox - How to bind CheckBox IsChecked to ListBox of CheckBoxes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM