grimpows
grimpows
CC#
Created by grimpows on 9/8/2022 in #help
Dynamic Binding for DataGrid WPF
following this article : https://paulstovell.com/dynamic-datagrid/ i'm stuck at the "Rendering basic columns in a DataGrid" section. I dont really see where i should put that code in a MVVM practice. should i create the entire datagrid in the VM ? should i trigger on some ItemsSource updated event in the code behind ? ... fixed with help of https://stackoverflow.com/questions/64687172/how-to-autogenerate-datagrid-columns-for-collection-of-child-type-from-collectio first i do create a Data class that contain my Records :
//the BindableBase is from prism but is same as implementation of INotifyPropertyChanged
public class Data : BindableBase
{
private ObservableCollection<Record> _records = new();
public ObservableCollection<Record> Records
{
get { return _records; }
set {
SetProperty(ref _records, value);
}
}
}
//the BindableBase is from prism but is same as implementation of INotifyPropertyChanged
public class Data : BindableBase
{
private ObservableCollection<Record> _records = new();
public ObservableCollection<Record> Records
{
get { return _records; }
set {
SetProperty(ref _records, value);
}
}
}
record :
public class Record
{
private readonly ObservableCollection<Property> properties = new ObservableCollection<Property>();

public Record(params Property[] properties)
{
foreach (var property in properties)
Properties.Add(property);
}

public ObservableCollection<Property> Properties
{
get { return properties; }
}


}
public class Record
{
private readonly ObservableCollection<Property> properties = new ObservableCollection<Property>();

public Record(params Property[] properties)
{
foreach (var property in properties)
Properties.Add(property);
}

public ObservableCollection<Property> Properties
{
get { return properties; }
}


}
property
public class Property
{
public Property(string name, object value)
{
Name = name;
Value = value;
}

public string Name { get; private set; }
public object Value { get; set; }

}
public class Property
{
public Property(string name, object value)
{
Name = name;
Value = value;
}

public string Name { get; private set; }
public object Value { get; set; }

}
in your VM you can instanciate your prop with
public class SomeViewModel: ViewModelBase
{
private Data _data = new();
public Data Data
{
get { return _data; }
set { SetProperty(ref _data, value); }
}
}
public class SomeViewModel: ViewModelBase
{
private Data _data = new();
public Data Data
{
get { return _data; }
set { SetProperty(ref _data, value); }
}
}
60 replies
CC#
Created by grimpows on 8/18/2022 in #help
Binding to Dictionary in WPF with a binding KEY
hello i have a very specific problem related to WPF : i have a listbox with itemsSource <ListBox ItemsSource="{Binding someKeyValuePairList}">
</ListBox> i could show the Key with <ListBox ItemsSource="{Binding someKeyValuePairList}"> <ListBox.ItemTemplate> <DataTemplate> <Grid> <TextBlock Margin="2" Text="{Binding Key}" /> .... </ListBox> and i also have some Dictionary where i want to access value who contain a ViewModel with the Key i have <ListBox ItemsSource="{Binding someKeyValuePairList}"> <ListBox.ItemTemplate> <DataTemplate> <Grid> ---> if i hardcode the key this working <ContentControl Content="{Binding DataContext.SomeDictionaryWithObject[test].ViewModel, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"/> ----> if i want to use the Value {Binding Key} this dont work ... <ContentControl Content="{Binding DataContext.SomeDictionaryWithObject[{Binding Key}].ViewModel, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"/> </Grid> </DataTemplate>
</ListBox.ItemTemplate> </ListBox> (for information i also have setted a DataTemplate for my ViewModel so the content control render the associated view)
1 replies