WPF Gridview复选框列标题MVVM

时间:2012-07-18 03:03:57

标签: c# wpf mvvm

我仍然围绕着整个MVVM模式,但我想我已经很好地掌握了它,直到我试图为我的Gridview创建一个Checkbox列。我需要用户能够选择列出的所有项目(通过标题复选框)或选择单独列出的项目。我将我的复选框的IsChecked属性数据绑定到我的viewmodel上的两个布尔字段。单元格模板上的复选框按预期工作,并触发属性更改事件。标题什么都不做。我在这里缺少什么这对我来说仍然是新的,所以要温柔。此外,如果有什么我应该做的,或更好的方式来实现这一点......我的耳朵。

由于

XAML

 <UserControl x:Class="CheckBoxDemo.GridDemo"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <ListView ItemsSource="{Binding PersonList}">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="50">
                    <GridViewColumn.HeaderTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding IsMainSelected}"/>
                        </DataTemplate>
                    </GridViewColumn.HeaderTemplate>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding IsSelected}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="100"></GridViewColumn>
            </GridView>
        </ListView.View>


    </ListView>
</Grid>

视图模型

class GridDemoViewModel:INotifyPropertyChanged
{
    public List<Person> PersonList { get; private set; }
    // Fields...
    private bool _isMainSelected;

    public bool IsMainSelected
    {
        get { return _isMainSelected; }
        set
        {
            _isMainSelected = value;
            NotifyPropertyChanged("IsMainSelected");

        }
    }

    public GridDemoViewModel()
    {
        PersonList = new List<Person>();
        PersonList.Add(new Person { Name = "John"});
        PersonList.Add(new Person { Name = "Tom" });
        PersonList.Add(new Person { Name = "Tina" });
        PersonList.Add(new Person { Name = "Mary" });
        PersonList.Add(new Person { Name = "Mia"});
        PersonList.Add(new Person { Name = "Crystal" });


    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

人员类

public class Person:INotifyPropertyChanged
{
    public string Name { get; set; }
    // Fields...
    private bool _isSelected;

    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            if (_isSelected == value)
                return;
            _isSelected = value;
            NotifyPropertyChanged("IsSelected");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

1 个答案:

答案 0 :(得分:2)

问题是GridViewColumn不是Visual Tree的一部分。这意味着它不会继承父ListView的DataContext。您必须找到一些引用ViewModel的其他方法。 Check out Josh Smith's DataContextSpy允许您轻松引入“人工继承的”DataContext

<UserControl.Resources>
     <spy:DataContextSpy x:Key="Spy" />
</UserControl.Resources>

<DataTemplate>
   <CheckBox IsChecked="{Binding Source={StaticResource Spy} Path=DataContext.IsMainSelected}"/>
</DataTemplate>