【问题标题】:WPF Gridview Checkbox Column Header MVVMWPF Gridview 复选框列标题 MVVM
【发布时间】:2012-07-18 03:03:57
【问题描述】:

我仍在思考整个 MVVM 模式,但我认为我已经很好地掌握了它,直到我尝试为我的 Gridview 创建一个复选框列。我需要用户能够选择列出的所有项目(通过标题复选框)或选择单独列出的项目。我将复选框的 IsChecked 属性数据绑定到我的视图模型上的两个布尔字段。单元格模板上的复选框按预期工作并触发属性更改事件。标题什么都不做。我在这里想念什么。再次,这对我来说仍然是新的,所以要温柔。此外,如果有什么我应该做的,或者更好的方法来完成这个……我全神贯注。

谢谢

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

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    问题在于 GridViewColumn 不是可视树的一部分。这意味着它不继承父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>
    

    【讨论】:

    • 感谢您的链接 - 在过去的几年里,我已经多次解决了同样的问题,但不知何故错过了这个!
    • 谢谢。这很好用。我使用了三者中更简单的方法,并将数据上下文添加到 app.resources。
    • 关于选择所有行的正确方法的任何建议。我见过几种方法,但它们看起来很老套,不如 MVVM 模式那么优雅。
    猜你喜欢
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 2014-02-16
    • 2011-08-23
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多