Title may be mistaken, but not, please keep reading.
I need a dynamic datagrid, so I'm creating the columns by using this method which gets the "selected" columns to display:
public void ConfigureGridColumns() { App.Instance.MainWindow.grdDetail.mainGrd.Columns.Clear(); var stats = new List<Statistic>(); stats.AddRange(App.Instance.Manager.PersonalizeStatistics.OrderBy(x => x.Index).Where(x => x.Selected).Select(x => x.Statistic)); foreach (var stat in stats) { var column = new DataGridTextColumn { Header = stat.Name, Binding = new Binding(stat.Key) }; } }
Now, I DO NOT WANT to create a model that contains ALL the columns, because I have more than 150 dynamic columns.
So I'm dynamically generating the ItemsSource by building a custom DataTable where row's column is mapped by the key like previous:
private void ConfigureGridSource() { var dt = new DataTable("stats"); foreach (var player in App.Instance.Manager.Players.Values.Take(5)) { var row = dt.NewRow(); foreach (var stat in stats) { if (!dt.Columns.Contains(stat.Statistic.Key)) t.Columns.Add(stat.Statistic.Key, typeof(StatisticPersonalizedValue)); var persStat = new StatisticPersonalizedValue(stat); persStat.Value = player.Statistics[stat.Statistic.Key]; row[stat.Statistic.Key] = persStat; } dt.Rows.Add(row); } grdDetail.mainGrd.ItemsSource = CollectionViewSource.GetDefaultView(dt.DefaultView); }
Issue is that:
with
Binding = new Binding(stat.Key)
I get the namespace of StatisticPersonalizedValue as text
with
Binding = new Binding("Value")
I get empty string (Value is the property of StatisticPersonalizedValue which is being valued in the second method)
I'm using models because I also have setters for positive and negative values in case, do you have any idea on how to fix this?
Answer: 1
Ok, fixed that.
I had to add a custom converter
Binding = new Binding(stat.Key) { Converter = new StatisticPersonalizedValueConverter() } public class StatisticPersonalizedValueConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value is StatisticPersonalizedValue) { return (value as StatisticPersonalizedValue).Value; } return string.Empty; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
This did the trick.
by : Luca Trazzihttp://stackoverflow.com/users/127041
No comments:
Post a Comment
Send us your comment related to the topic mentioned on the blog