1
我仍然圍繞着這整個MVVM模式,但我認爲我有一個很好的把握,直到我試圖爲我的Gridview創建一個複選框列。我需要用戶能夠選擇列出的所有項目(通過標題複選框)或選擇單獨列出的項目。我將我的複選框的IsChecked屬性綁定到我的viewmodel上的兩個布爾字段。單元格模板上的複選框按預期工作,並觸發屬性更改的事件。標題什麼都不做。我在這裏錯過了什麼。再次,這對我來說仍然是新的,所以要溫柔。此外,如果有什麼我應該做的,或更好的方式來實現這一目標......所有的耳朵。WPF Gridview複選框列標題MVVM
由於
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));
}
}
}
謝謝 - 我已經解決了同樣的問題,在過去幾年中多次,但不知錯過了! – Scott 2012-07-18 06:13:26
謝謝。這很好。我使用了三者中較爲簡單的方法,並將數據上下文添加到app.resources中。 – 2012-07-18 13:32:01
有關正確選擇所有行的任何建議。我見過一些方法,但看起來很詭異,並不像MVVM模式那樣優雅。 – 2012-07-18 13:42:24