Динамическое создание GridView из ObservableCollectionC#

Место общения программистов C#
Ответить
Anonymous
 Динамическое создание GridView из ObservableCollection

Сообщение Anonymous »


В моем приложении C#/WPF у меня есть

Код: Выделить всё

ListView
that I want to use as a

Код: Выделить всё

GridView
to display items in a table: It is binded to a

Код: Выделить всё

ObservableCollection
, where

Код: Выделить всё

Condition
is defined like this:

Код: Выделить всё

public class Condition
{
public string Name { get; set; }
public ObservableCollection Properties { get; set; }

public class Property
{
public string Name { get; set; }
public double Value { get; set; }
}
}
(Please note that I skipped

Код: Выделить всё

INotifyPropertyChanged
implementation here to simplify this example code).
Idea is that GridView Column headers should display property of instances of

Код: Выделить всё

Condition
, while row headers should display instance of Property, and the cells - the . It is expected that all

Код: Выделить всё

Condition
instances will have the same number of Property instances with the same names. So for example if we have this method that creates sample data:

Код: Выделить всё

private ObservableCollection CreateConditions()
{
ObservableCollection conditions = new();

conditions.Add(
new Condition()
{
Name = "c1",
Properties = new()
{
new() { Name = "p1", Value = 5 },
new() { Name = "p2", Value = 10 },
new() { Name = "p3", Value = 15 },
}
});

conditions.Add(
new Condition()
{
Name = "c2",
Properties = new()
{
new() { Name = "p1", Value = 8 },
new() { Name = "p2", Value = 12 },
new() { Name = "p3", Value = 20 },
}
});

conditions.Add(
new Condition()
{
Name = "c3",
Properties = new()
{
new() { Name = "p1", Value = 11 },
new() { Name = "p2", Value = 17 },
new() { Name = "p3", Value = 25 },
}
});

return conditions;
}
Then the GridView table should look like this:

Код: Выделить всё

-   c1  c2  c3
p1  5   8   11
p2  10  12  17
p3  15  20  25
Since I'm trying to use MVVM, the view model looks like this:

Код: Выделить всё

public class TableResultsViewViewModel
{
public ObservableCollection Conditions { get; set; }

public TableResultsViewViewModel(ObservableCollection conditions)
{
this.Conditions = conditions;
}
}
And the main class:

Код: Выделить всё

public partial class MainWindow : Window
{
private TableResultsViewViewModel vm;

public MainWindow()
{
InitializeComponent();
ObservableCollection conditions = CreateConditions(); //sample data creation method from before
vm = new TableResultsViewViewModel(conditions);
this.DataContext = vm;
}
}
But I can't figure out how to properly declare the ListView in WPF so that it would dynamically generate these columns and rows as the

Код: Выделить всё

ObservableCollection Conditions
changes (instances of

Код: Выделить всё

Condition
added or removed from this collection, or instances of

Код: Выделить всё

Property
added or removed from

Код: Выделить всё

Properties
list inside the instances of

Код: Выделить всё

Condition
). Has anyone done anything similar and could help me out?


Источник: https://stackoverflow.com/questions/781 ... collection
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»