【问题标题】:How do I bind IsChecked property of CheckBox within an ItemsControl?如何在 ItemsControl 中绑定 CheckBox 的 IsChecked 属性?
【发布时间】:2012-06-20 02:48:11
【问题描述】:

我有以下 ItemsControl,它为可用集合中的每个数据库提供了一个复选框。这些复选框允许用户选择要过滤的复选框。要过滤的数据库位于单独的集合 (FilteredDatabases) 中。我该怎么做?我可以将 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;
}

【问题讨论】:

  • 您需要在视图模型上添加一个属性,您将使用该属性的设置器从 FilteredDatabases 集合中添加或删除选中的数据库
  • 问题是我不知道在我的项目控制中检查了哪个项目。如何将其添加到我的视图模型中?

标签: wpf binding checkbox itemscontrol


【解决方案1】:
  1. 将 CheckBox.Command 绑定到路由命令实例
  2. 将路由命令绑定到方法
  3. 使用 IBindingList.Add 和 IBindingList.Remove 方法

【讨论】:

  • 这让我更进一步。现在选中/取消选中该框会从过滤的集合中添加/删除项目。但是,如何初始化视图,以便在首次显示表单时检查过滤集合中的数据库。同样,我想用绑定/MVVM 来做到这一点。
【解决方案2】:

以下代码说明了您正在尝试做什么,为了做到这一点,您最好使用 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,我省略了它,因为在这种特殊情况下没有必要。

【讨论】:

    猜你喜欢
    • 2011-06-16
    • 2016-04-26
    • 1970-01-01
    • 2015-07-24
    • 2012-11-15
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    相关资源
    最近更新 更多