Change ItemsControl grouping dynamically - wpf

I've search over the Internet for changing ItemsControl dynamically, but no luck (maybe my query is not good enough), so I ask here. Anyone can help me about creating grouping feature for an ItemsControl that can change group type dynamically.
Example, I have a collection of songs which have properties: Name, Artirst, Singer, Album. Then I have 3 radio buttons to which property is used to group there songs.
I have thinked about creating multiple CollectionView, but I think there is another ways which is better.
(I'm new at WPF, so whould you please give a little more details answer. Thank you :))

Here is the most minimal example I can come up with in MVVM style.
The viewmodel:
enum GroupBy
{
Name,
Artist
}
class Song
{
public string Name { get; set; }
public string Artist { get; set; }
}
class SonglistViewModel : ViewModelBase
{
private GroupBy groupBy;
public GroupBy GroupBy
{
get => this.groupBy;
set
{
if(this.groupBy == value)
{
return;
}
this.groupBy = value;
this.UpdateGrouping(value);
this.OnPropertyChanged();
}
}
public ObservableCollection<Song> Songs { get; } = new ObservableCollection<Song>();
private void UpdateGrouping(GroupBy value)
{
var servicesView = CollectionViewSource.GetDefaultView(this.Services);
servicesView.GroupDescriptions.Clear();
switch (value)
{
case GroupBy.Name:
servicesView.GroupDescriptions.Add(
new PropertyGroupDescription(nameof(Song.Name)));
break;
case GroupBy.Artist:
servicesView.GroupDescriptions.Add(
new PropertyGroupDescription(nameof(Song.Artist)));
break;
}
}
}
The view:
<ListView ItemsSource="{Binding Songs}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Artist" DisplayMemberBinding="{Binding Artist}" />
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding ItemCount, StringFormat={}({0})}" />
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListViewGroupStyle>
</ListView>

Related

Wpf ListView Grouping

I've been trying to replicate the Listview like the screenshot below
enter image description here
But my problem is I can't make it work. I'm pretty new to MVVM
This is my xaml code
<ListView Margin="10,10,10,6" x:Name="BusinessListView">
<ListView.View>
<GridView>
<GridViewColumn Header="Company" DisplayMemberBinding="{Binding Company}" />
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock FontWeight="Bold" Text="{Binding BusinessType}"></TextBlock>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
And below is my viewmodel code
List<BusinessM> businesses = new List<BusinessM>();
businesses.Add(new BusinessM
{
BusinessType = "Enterprise",
ListBusiness = new List<BusinessInfo>
{
new BusinessInfo {Company = "Microsoft"}
}
});
BusinessListView.ItemsSource = businesses;
Model
public class BusinessM
{
public string BusinessType { get; set; }
public List<BusinessInfo> ListBusiness { get; set; }
public BusinessM()
{
ListBusiness = new List<BusinessInfo>();
}
}
public class BusinessInfo
{
public string Company { get; set; }
}
When I run the program it returns an error:
System.Windows.Data Error: 40 : BindingExpression path error: 'Company' property not found
What's wrong with my code? What I understand is I'm not referencing properly the property Company into the Listview?
If it's that's the case. How can I do it properly. I've been trying to solve it myself but I can't make it work.
Thank you.
You are getting the error specified because of the way your data structure is bounded to listview's item source property. Typically, you would have a flattened list of model and then you would use ColllectionViewSource to apply Grouping and/or Sorting on the collection.
In posted code, you are binding ItemSource to list of BusinessM which means each list view item represents object of type BusinessM. So when you bind GridViewColumn to Company,it expects BusinessM type to have a property with similar name, which by the way, is not the case.
I have modified you code so that I can explain you how it should be done (IMO) with example.
Model:
public class BusinessM
{
public string BusinessType { get; set; }
public string Company { get; set; }
}
Code behind (xaml.cs):
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CollectionViewSource cvs = new CollectionViewSource();
cvs.Source = GenerateData();
cvs.GroupDescriptions.Add(new PropertyGroupDescription(nameof(BusinessM.BusinessType)));
BusinessListView.ItemsSource = cvs.View;
}
private List<BusinessM> GenerateData()
{
List<BusinessM> businesses = new List<BusinessM>();
for (int i = 1; i <= 10; i++)
{
for (int j = 1; j <= 5; j++)
{
businesses.Add(new BusinessM
{
BusinessType = $"Type {i}",
Company = $"{i}{j}"
});
}
};
return businesses;
}
}
View (xaml):
<ListView Margin="10,10,10,6" x:Name="BusinessListView">
<ListView.View>
<GridView>
<GridViewColumn Header="" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Company" DisplayMemberBinding="{Binding Company}" />
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="True" Header="{Binding Name}">
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
</ListView>

Bind a custom class of controls to Listview/GridView as ListViewItem

I am facing some problem with binding the data to the controls in ListView/GridView as ListViweItem.
Requirement: Have to show a checkbox, Label, DropDown and a textbox per row which get data from all different sources.Here is the sample class:
class Data {
public CheckBox chk_box;
public Label lbl;
public ComboBox cmb_box;
public TextBox t_box;
public Data(String lbl_Data,List<String> cmb_box_Data) {
chk_box=new CheckBox();
lbl=new Label();
lbl.Content=lbl_Data;
cmb_box=new ComboBox();
cmb_box.ItemsSource=cmb_box_Data;
chk_box.Click += new RoutedEventHandler(chk_box_clicked);
...
}
private void chk_box_clicked(object Sender, RoutedEventArgs e){
if(chk_box.IsChecked == true) cmb_box.IsEnabled = false;
else cmb_box.IsEnabled = true;
}
}
Now i need to add this class' object as a ListViewItem, per control per column in ListView/GridView - dynamically, row by row and then after user makes selections access all the rows once again row by row and get values from these controls.
XAML code:
<ListView x:Name="TestGrid">
<ListView.View>
<GridView>
<GridViewColumn Header=" Select "/>
<GridViewColumn Header=" Label "/>
<GridViewColumn Header=" cmb_box "/>
<GridViewColumn Header=" t_box "/>
</GridView>
</ListView.View>
</ListView>
Since, the Items Source for all the controls is different, i cannot use ListView.ItemsSorce to bind data. Please suggest suitable options, or ways to accomplish the desired UI. I would like to make everything out from the controls given in VS only without using a third party dll(if possible).
Thank you for your time and suggestions in advance.
Check msdn.microsoft.com for binding
public class Datum
{
public bool Select { get; set; }
public string Name { get; set; }
public List<string> Items { get; set; }
}
<ListView ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Select">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=Select, Mode=OneWay}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name, Mode= OneWay}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=Items}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>

WPF ListView grouping by 2 columns but display only 1 group header

A ListView displays a collection of the following class:
public class Employee
{
private string _department;
private string _manager;
private string _name;
private string _address;
public string Department
{
get { return _department; }
}
public string Manager
{
get { return _manager; }
}
public string Name
{
get { return _name; }
}
public string Address
{
get { return _address; }
}
}
There is a 1-to-1 relation between Department and Manager, so any 2 rows with the same department will also have the same manager.
I want to group by Department/Manager, with the group header showing "Department (Manager)".
My CollectionViewSource looks like
<CollectionViewSource x:Key="cvsEmployees" Source="{Binding Employees}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Department" />
<PropertyGroupDescription PropertyName="Manager" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
The plan is to not display the first level header (Department) and to somehow bind to both the Department (1st level) and the Manager (2nd level) from the 2nd level header.
3 questions:
To avoid displaying the 1st level header, I have an empty data template in the groupstyle:
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
This seems very clunky. Is there a more elegant way to skip a group header?
How do I bind to the 1st grouping level property (Department) from the 2nd level header (Manager) to achieve the required "Department (Manager)" ?
Is there a better way to do this than creating 2 grouping level?
Thanks
Solved the main stumbling block, question 2 above: how to bind from the group header to a property that is not the grouping property.
The solution is to change the data context to:{Binding Items}. The ItemSource properties are then available
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,10,0,3" DataContext="{Binding Items}" >
<TextBlock Text="{Binding Path=Department}" FontWeight="Bold" Margin="3"/>
<TextBlock Text="{Binding Path=Manager, StringFormat='({0})'}" Margin="3"/>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
I would create another model part, which represents the dual grouping that you need to have happen:
Model Classes:
public class EmployeeModel {
private readonly Employee _Employee;
public DepartmentManager ManagementInfo { get; private set; }
public string Name {
get { return _Employee.Name; }
}
public string Address {
get { return _Employee.Address; }
}
public EmployeeModel(Employee employee) {
this._Employee = employee;
this.ManagementInfo = new DepartmentManager(employee.Department, employee.Manager);
}
}
public class DepartmentManager {
public string Department { get; private set; }
public string Manager { get; private set; }
public DepartmentManager(string dept, string manager) {
this.Department = dept;
this.Manager= manager;
}
public override bool Equals(object obj) {
var model = obj as DepartmentManager;
if(null == model)
return false;
return Department.Equals(model.Department, StringComparison.InvariantCultureIgnoreCase) &&
Manager.Equals(model.Manager, StringComparison.InvariantCultureIgnoreCase);
}
}
XAML:
<CollectionViewSource x:Key="cvsEmpsModel" Source="{Binding EmployeesModel}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="ManagementInfo" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<DataTemplate DataType="{x:Type models:EmployeeModel}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding Address}" />
</StackPanel>
</DataTemplate>
...
<ListView ItemsSource="{Binding Source={StaticResource cvsEmpsModel}}">
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,10,0,3" DataContext="{Binding Items}">
<TextBlock Text="{Binding Path=ManagementInfo.Manager}" FontWeight="Bold" Margin="3" />
<TextBlock Text="{Binding Path=ManagementInfo.Department, StringFormat='({0})'}" Margin="3" />
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
Then in your Window/ViewModel:
this.EmployeesModel = new ObservableCollection<EmployeeModel>(MyListOfEmployersFromDB.Select(e => new EmployeeModel(e)));
Note, I've overriden Equals in the DepartmentManager class, but not GetHashCode, ideally you should do a custom implementation of that. I had to override equals so the grouping view source would correctly group the same entries. You could get rid of this need, buy constructing the DepartmentManager for the same Employees outside of the collection, and pass them into the EmployeeModel ctr.

Binding ObservableCollection of ObservableCollections to nested ListView in TabControl TabItems

Rather than declaring a tab for each of the ObservableCollections explicitly as in the first TabControl below I need them to generate dynamically as in the second TabControl and have the ItemsSource of the nested ListView set to each of the nested ObservableCollections.
In other words: Why aren't the ItemSource bindings of the nested ListViews in the second TabControl working? Is there a way to set the index of the nested ObservableCollection to the index of the containing ObservableCollection?
Or: How do I make the second dynamic TabControl and nested ListViews look like the first static TabControl and nested ListViews?
using System.Collections.ObjectModel;
using System.Windows;
namespace GridViewColumns2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ViewModel viewModel = new ViewModel();
viewModel.ThingCollections = new ObservableCollection<ThingCollection>();
viewModel.ThingCollections.Add(new ThingCollection { Name = "One" });
viewModel.ThingCollections[0].Things = new ObservableCollection<Thing>();
viewModel.ThingCollections[0].Things.Add(new Thing { Name = "One.One" });
viewModel.ThingCollections[0].Things.Add(new Thing { Name = "One.Two" });
viewModel.ThingCollections[0].Things.Add(new Thing { Name = "One.Three" });
viewModel.ThingCollections.Add(new ThingCollection { Name = "Two" });
viewModel.ThingCollections[1].Things = new ObservableCollection<Thing>();
viewModel.ThingCollections[1].Things.Add(new Thing { Name = "Two.One " });
viewModel.ThingCollections[1].Things.Add(new Thing { Name = "Two.Two" });
viewModel.ThingCollections[1].Things.Add(new Thing { Name = "Two.Three" });
DataContext = viewModel;
}
}
public class ViewModel
{
public ObservableCollection<ThingCollection> ThingCollections { get; set; }
}
public class ThingCollection
{
public string Name { get; set; }
public ObservableCollection<Thing> Things { get; set; }
}
public class Thing
{
public string Name { get; set; }
}
}
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TabControl>
<TabItem>
<TabItem.Header>One</TabItem.Header>
<TabItem.Content>
<ListView ItemsSource="{Binding Path=ThingCollections[0].Things}">
<ListView.View>
<GridView>
<GridViewColumn Width="120" Header="Name" DisplayMemberBinding="{Binding Name}" />
</GridView>
</ListView.View>
</ListView>
</TabItem.Content>
</TabItem>
<TabItem>
<TabItem.Header>Two</TabItem.Header>
<TabItem.Content>
<ListView ItemsSource="{Binding Path=ThingCollections[1].Things}">
<ListView.View>
<GridView>
<GridViewColumn Width="120" Header="Name" DisplayMemberBinding="{Binding Name}" />
</GridView>
</ListView.View>
</ListView>
</TabItem.Content>
</TabItem>
</TabControl>
<TabControl Grid.Column="1" ItemsSource="{Binding Path=ThingCollections}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding Path=ThingCollections[0].Things}">
<ListView.View>
<GridView>
<GridViewColumn Width="120" Header="Name" DisplayMemberBinding="{Binding Name}" />
</GridView>
</ListView.View>
</ListView>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Grid>
When you're in a DataTemplate, the DataContext is already that of the item (in your case, a ThingCollection class.
So instead of:
<ListView ItemsSource="{Binding Path=ThingCollections[0].Things}">
Use:
<ListView ItemsSource="{Binding Path=Things}">
To diagnose future binding errors, hit F5 (start debugging) and then take a look in the Output Window. All binding errors will be recorded there. In this case, it would've indicated that it cannot find a property named ThingCollections in the object ThingCollection.
Remove ThingCollections[0]. in BindingPath. The DataContext will be set for you to the right ThingCollection.
<ListView ItemsSource="{Binding Path=Things}">

Button inside a WPF List view/data grid

I am trying to get the value/ID of the clicked row. If the row is selected, this works fine. But if I just try to click the button inside, the selected customer is null. How do I do Command Parameters here.
I tried this see the answers to the following questions:
ListView and Buttons inside ListView
WPF - Listview with button
Here is the code:
<Grid>
<ListView ItemsSource="{Binding Path=Customers}"
SelectedItem="{Binding Path=SelectedCustomer}"
Width="Auto">
<ListView.View>
<GridView>
<GridViewColumn Header="First Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Margin="6,2,6,2">
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Address">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Margin="6,2,6,2">
<Button Content="Address" Command="{Binding Path=DataContext.RunCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
public class VM : ViewModelBase
{
public RelayCommand RunCommand { get; private set; }
private ObservableCollection<Customer> _Customers;
public ObservableCollection<Customer> Customers
{
get { return _Customers; }
set
{
if (value != _Customers)
{
_Customers = value;
RaisePropertyChanged("Customers");
}
}
}
private Customer _SelectedCustomer;
public Customer SelectedCustomer
{
get { return _SelectedCustomer; }
set
{
if (value != _SelectedCustomer)
{
_SelectedCustomer = value;
RaisePropertyChanged("SelectedCustomer");
}
}
}
public VM()
{
Customers = Customer.GetCustomers();
RunCommand = new RelayCommand(OnRun);
}
private void OnRun()
{
Customer s = SelectedCustomer;
}
}
in the OnRun Method, selected Customer is coming in as null. I want the data row(customer) here. How do I do this?
Three possible solutions that you can choose.
Pass current row object as a CommandParameter. In this case, you will modify OnRun method a little. (recommended)
<Button Content="Address" Command="{Binding Path=DataContext.RunCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" CommandParameter={Binding} />
I think CommandParameter={Binding} works fine because the DataContext of each row is each Customer object.
and OnRun method needs to be modified in order to get a parameter as an argument.
private void OnRun(object o){
if(!(o is Customer)) return;
// Do something
}
Or, Write a little code-behind with SelectionChanged event handling. (not recommended)
Or, Use EventToCommand in MVVM-light toolkit. (not recommended)
CommandParameter does NOT entirely support data binding.
http://connect.microsoft.com/VisualStudio/feedback/details/472932/wpf-commandparameter-binding-should-be-evaluated-before-command-binding

Resources