WPF MVVM Bind Dictionary <String, List <String> for datagrid

I am having trouble linking my Dictionary<string, List<string>>WPat datagrid to my correct location.

This is my data object:

public class Result
{
    public Result(string type, string name, Dictionary<string, List<string>> partners)
    {
        Type = type;
        Name = name;
        Partners = partners;
    }

    public string Type { get; set; }
    public string Name { get; set; }
    public Dictionary<string, List<string>> Partners { get; set; }
}

What I filled in with dummy data and exposed as a public property of my view model:

public class MainViewModel
{
    public MainViewModel()
    {   
        var partners = new Dictionary<string, List<string>>
        {
            {"Company", new List<string> {"company1", "company2"}},
            {"Operator", new List<string> {"false", "true"}},
            {"Interest", new List<string> {"40%", "60%"}},
            {"Type", new List<string> {"type1", "type2"}}
        };
        Asset = new Result("TestType", "TestName", partners);
    }

    public Result Asset { get; set; }
}

I am trying to bind a dictionary Partnersto my datagrid. Keyeach dictionary entry (e.g. Company / Operator / Interest / Type) should have the headers of my datagrid with the corresponding values ​​filling the columns. Like this:

Company | Operator | Interest | Type
company1   false       40%      type1
company2   true        60%      type2

? , , . , datagrid ( , , ). XAML , itemsource , , :

<DataGrid x:Name="dg" ItemsSource="{Binding Asset.Partners}" AutoGenerateColumns="false">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Company" Binding="{Binding ?}"/>
        <DataGridTextColumn Header="Operator" Binding="{Binding ?}"/>
        <DataGridTextColumn Header="Interest" Binding="{Binding ?}"/>
        <DataGridTextColumn Header="Type" Binding="{Binding ?}"/>
    </DataGrid.Columns>
</DataGrid>

Binding="{Binding Value[0]} , "" , , . Binding="{Binding Asset.Partners[Company]}", .

.

+4
2

.

public class MyObject
{
    public string Company { get; set; }
    public string Operator { get; set; }
    public string Interest { get; set; }
    public string Type { get; set; }
}

public MainViewModel()
{   
    var partners = new ObservableCollection<MyObject>()
    {
        new MyObject("company1", "false", "40%", "type1"),
        new MyObject("company2", "true", "60%", "type2")
    };
    ...
}

, , , DataGrid, " , DataRow DataContext ". , .

+3

@courlu , ObservableCollection.   IEnumerable ICollection, , ItemSource wpf.

, .Net: : IDictionary, ICollection>, IEnumerable>, IEnumerable, IDictionary, ICollection, IReadOnlyDictionary, IReadOnlyCollection>, ISerializable, IDeserializationCallback   {    }

<DataGrid x:Name="dg" ItemsSource="{Binding Asset.Partners.Values}" AutoGenerateColumns="false">
<DataGrid.Columns>
    <DataGridTextColumn Header="Company" Binding="{Binding Company}"/>
    <DataGridTextColumn Header="Operator" Binding="{Binding Operator}"/>
    <DataGridTextColumn Header="Interest" Binding="{Binding Interest}"/>
    <DataGridTextColumn Header="Type" Binding="{Binding Type}"/>
</DataGrid.Columns>

TwoWay, 2 INotifyCollectionChanged, INotifyPropertyChanged , ObservableCollection.

0

All Articles