Reputation: 22810
I'm definitely not new to C# and .NET, but since I haven't played with XAML for a quite a long time, I'm facing a pretty simple issue...
I've created a test Model (ResultsModel
) like this :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace Test_Project_
{
public class ResultsModel : INotifyPropertyChanged
{
private string _Monthly;
public string Monthly
{
get { return _Monthly; }
set { _Monthly = value; NotifyPropertyChanged("Monthly"); }
}
private string _TotalAmount;
public string TotalAmount
{
get { return _TotalAmount; }
set { _TotalAmount = value; NotifyPropertyChanged("TotalAmount"); }
}
private string _TotalInterest;
public string TotalInterest
{
get { return _TotalInterest; }
set { _TotalInterest = value; NotifyPropertyChanged("TotalInterest"); }
}
List<Dictionary<string, double>> _Items;
public List<Dictionary<string, double>> Items
{
get { return _Items; }
set { _Items = value; NotifyPropertyChanged("Items"); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
}
}
In my MainPage
class :
public ResultsModel Results;
public MainPage()
{
InitializeComponent();
Results = new ResultsModel();
DataContext = Results;
}
Now, here's the catch :
When I'm trying to bind e.g. a TextBlock
's Text
property to some of the simple string values of my Model (e.g. Text={Binding Monthly}
), it works.
Now, please note that I also have a grid, with four columns.
I want to take all the items in my Items
Property (ResultsModel
) and show them in the grid by key.
E.g.
Show List item's "A" key, in column "A" (let's say the first column of the grid), etc.
How can I do that (in XAML and/or C# code)?
UPDATE :
(based on suggestions, I tried the following, but still not working)
Inside Grid
XAML :
<ItemsControl ItemsSource="{Binding Items, Mode=TwoWay}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Grid.Column="0" Text="{Binding Path=num}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
But I'm getting the following error :
System.Windows.Data Error: BindingExpression path error: 'num' property not found on 'System.Collections.Generic.Dictionary
2[System.String,System.String]' 'System.Collections.Generic.Dictionary
2[System.String,System.String]' (HashCode=57616766). BindingExpression: Path='num' DataItem='System.Collections.Generic.Dictionary`2[System.String,System.String]' (HashCode=57616766); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..
Also, please not that Items
has been converted to a List of string-string key-value pairs.
Any ideas?
Upvotes: 0
Views: 117
Reputation: 1438
Did you just try this in your binding :
{Binding Monthly, Mode = TwoWay}
In both of column definition and textBox.
When you writing your columns, you must have something like this :
<datagrid.column>
<datagridtextColumn header
binding />
</datagrid.column>
If I remeber well, you can make binding on column.
This must link value between textBlock and Column.
This can resolve your issue.
This works for me.
Upvotes: 0
Reputation: 22435
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemTemplate>
<!-- datatemplate to render your items -->
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanelTemplate>
<!-- wrappanel or what ever to control orientation and stuff -->
</ItemsControl.ItemsPanelTemplate>
Upvotes: 3