带MVVM的WPF DataGrid.GroupStyle
我尝试列出几个数据项,这些数据项应在三个不同的组中可视化:带MVVM的WPF DataGrid.GroupStyle,wpf,mvvm,wpfdatagrid,Wpf,Mvvm,Wpfdatagrid,我尝试列出几个数据项,这些数据项应在三个不同的组中可视化: 仪表板 内务管理 科学 使用MVVM(模型视图模型)设计模式,我创建了两个模型类: DataGroup.cs public class DataGroup { public String Name { get; set; } } public class DataItem { public String Name { get; set; } public String Format { get; set;
- 仪表板
- 内务管理
- 科学
public class DataGroup
{
public String Name { get; set; }
}
public class DataItem
{
public String Name { get; set; }
public String Format { get; set; }
public DataGroup Group { get; set; }
}
class DataListViewModel : ViewModelBase
{
public List<Models.DataGroup> GroupList { get; set; }
public List<Models.DataItem> DataList { get; set; }
public DataListViewModel()
{
var GroupList = new List<Models.DataGroup>
{
new Models.DataGroup
{
Name = "Dashboard"
},
new Models.DataGroup
{
Name = "Housekeeping"
},
new Models.DataGroup
{
Name = "Science"
}
};
var DataList = new List<Models.DataItem>
{
new Models.DataItem
{
Name = "Panel A",
Format = "N/A",
Group = GroupList[0],
},
new Models.DataItem
{
Name = "Panel B",
Format = "N/A",
Group = GroupList[0],
},
new Models.DataItem
{
Name = "Panel C",
Format = "N/A",
Group = GroupList[0],
},
new Models.DataItem
{
Name = "+3.4 V",
Format = "TBD",
Group = GroupList[1],
},
new Models.DataItem
{
Name = "+5.3 V",
Format = "TBD",
Group = GroupList[1],
DataItem.cs
public class DataGroup
{
public String Name { get; set; }
}
public class DataItem
{
public String Name { get; set; }
public String Format { get; set; }
public DataGroup Group { get; set; }
}
class DataListViewModel : ViewModelBase
{
public List<Models.DataGroup> GroupList { get; set; }
public List<Models.DataItem> DataList { get; set; }
public DataListViewModel()
{
var GroupList = new List<Models.DataGroup>
{
new Models.DataGroup
{
Name = "Dashboard"
},
new Models.DataGroup
{
Name = "Housekeeping"
},
new Models.DataGroup
{
Name = "Science"
}
};
var DataList = new List<Models.DataItem>
{
new Models.DataItem
{
Name = "Panel A",
Format = "N/A",
Group = GroupList[0],
},
new Models.DataItem
{
Name = "Panel B",
Format = "N/A",
Group = GroupList[0],
},
new Models.DataItem
{
Name = "Panel C",
Format = "N/A",
Group = GroupList[0],
},
new Models.DataItem
{
Name = "+3.4 V",
Format = "TBD",
Group = GroupList[1],
},
new Models.DataItem
{
Name = "+5.3 V",
Format = "TBD",
Group = GroupList[1],
…一个ViewModel类:
DataListViewModel.cs
public class DataGroup
{
public String Name { get; set; }
}
public class DataItem
{
public String Name { get; set; }
public String Format { get; set; }
public DataGroup Group { get; set; }
}
class DataListViewModel : ViewModelBase
{
public List<Models.DataGroup> GroupList { get; set; }
public List<Models.DataItem> DataList { get; set; }
public DataListViewModel()
{
var GroupList = new List<Models.DataGroup>
{
new Models.DataGroup
{
Name = "Dashboard"
},
new Models.DataGroup
{
Name = "Housekeeping"
},
new Models.DataGroup
{
Name = "Science"
}
};
var DataList = new List<Models.DataItem>
{
new Models.DataItem
{
Name = "Panel A",
Format = "N/A",
Group = GroupList[0],
},
new Models.DataItem
{
Name = "Panel B",
Format = "N/A",
Group = GroupList[0],
},
new Models.DataItem
{
Name = "Panel C",
Format = "N/A",
Group = GroupList[0],
},
new Models.DataItem
{
Name = "+3.4 V",
Format = "TBD",
Group = GroupList[1],
},
new Models.DataItem
{
Name = "+5.3 V",
Format = "TBD",
Group = GroupList[1],
我能够将数据列表可视化,但挖出它们却不起作用。如何更改DataListViewModel中的代码 要对这样的列表进行分组,需要使用CollectionViewSource 将其添加到您的资源中,如下所示:
<CollectionViewSource x:Key="GroupedDataList" Source="{Binding DataList}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Group" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
GroupStyle
显示也有点不对劲-您不需要将HeaderTemplate
与您应用的ContainerStyle
放在一起。此外,您还需要将第一个文本块的绑定
更改为Name.Name
:
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name.Name}" FontWeight="Bold" Foreground="{DynamicResource WhiteBrush}"/>
<TextBlock Text=" - " Foreground="{DynamicResource WhiteBrush}"/>
<TextBlock Text="{Binding Path=ItemCount}" Foreground="{DynamicResource WhiteBrush}"/>
</StackPanel>
但你们可以玩转绑定和放置,找出最适合你们的
哦,我做的最后一件事是删除顶部的ListView,它在我制作的示例应用程序中被完全覆盖了。但是,根据所包含的XAML的不同,情况可能不同
希望这有帮助 要对这样的列表进行分组,需要使用CollectionViewSource
将其添加到您的资源中,如下所示:
<CollectionViewSource x:Key="GroupedDataList" Source="{Binding DataList}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Group" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
GroupStyle
显示也有点不对劲-您不需要将HeaderTemplate
与您应用的ContainerStyle
放在一起。此外,您还需要将第一个文本块的绑定
更改为Name.Name
:
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name.Name}" FontWeight="Bold" Foreground="{DynamicResource WhiteBrush}"/>
<TextBlock Text=" - " Foreground="{DynamicResource WhiteBrush}"/>
<TextBlock Text="{Binding Path=ItemCount}" Foreground="{DynamicResource WhiteBrush}"/>
</StackPanel>
但你们可以玩转绑定和放置,找出最适合你们的
哦,我做的最后一件事是删除顶部的ListView,它在我制作的示例应用程序中被完全覆盖了。但是,根据所包含的XAML的不同,情况可能不同
希望这有帮助