0

我有以下 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;
}
4

2 回答 2

0

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

于 2012-06-19T18:48:11.663 回答
0
  1. 将 CheckBox.Command 绑定到路由命令实例
  2. 将路由命令绑定到方法
  3. 使用 IBindingList.Add 和 IBindingList.Remove 方法
于 2012-06-19T00:23:58.070 回答