I have a datagrid which has to be associated to an observable collection of the following class:
public class CfgCounters
{
public int valuePresent { get; set; }
public string Name { get; set; }
public ObservableCollection<DateTime> obcDatetime { get; set; }
public ObservableCollection<string> obcLastExecutedPP { get; set; }
public ObservableCollection<int> obcNumDimsOK { get; set; }
}
I have therefore
ObservableCollection<CfgCounters> obcCounters = new ObservableCollection<CfgCounters>();
which I fill with instances and then associate to the dtg with dtg.ItemsSource = obcCounters;
which obviously is not showing the obcs in any way. So the question is: how can I in code behind show (in any way would do) those observable collections using code behind only ? Thanks
You should transform each CfgCounters
object into an item with only scalar properties, eg:
ObservableCollection<CfgCounters> obcCounters = new ObservableCollection<CfgCounters>();
...
dtg.ItemsSource = obcCounters.Select(x => new
{
x.valuePresent,
x.Name,
obcDatetime = string.Join(",", x.obcDatetime.Select(y => y.ToString("yyyy-MM-dd"))),
obcLastExecutedPP = string.Join(",", x.obcLastExecutedPP),
obcNumDimsOK = string.Join(",", x.obcNumDimsOK)
}).ToArray();
Your data is hierarchical and your view is not. You maybe looking for:
TreeView to display hierarchical data as you have.
Or you can flatten the data and use this as the source of the datagrid. The code examples show a view model inherited from the data model but a conversion in code behind to a new type would also be possible.
Datagrid can also group lists. But this doesn't fit here, see 2.
When the csv doesn't change at run time:
public class CfgCountersViewModel : CfgCounters
{
public string obcDatetimeCsv => String.Join(",", obcDatetime);
...
}
Or when the childs of CfgCounters changes during run time, something more complex like:
public class CfgCountersViewModel : CfgCounters, INotifyPropertyChanged
{
public CfgCountersViewModel()
{
// UpdateObcDatetimeCsv when obcDatetime changes
obcDatetime.OnCollectionChanged += (sender, args) =>
{
obcDatetimeCsv = String.Join(",", obcDatetime);
}
}
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.