C
C#3w ago
Mormon Son

✅ Data Binding System.Numeric.Vector3?

I have a collection of objects containing Vector3 properties. Is it possible to data bind directly to the X/Y/Z fields in my WPF xaml data grid or do I need to add pass through properties for them all? Seems like a lot of boilerplate when you have many vectors.
10 Replies
Evyr
Evyr3w ago
not with properties. It's a value type, so the getter gets a copy, and trying to set a field of that retrieved copy won't affect the property itself
Mormon Son
Mormon SonOP3w ago
I’m only interested in reading the values, but it would be nice not to add 3 additional properties for each of the 7 vectors on my data object to read them all So I’m wondering if there is some advanced way I can enable binding directly to the field rather than a property, perhaps something to do with ICustomTypeDescriptor? TypeDescriptorProvider can only be applied to classes I control, is there any equivalent way to provide a custom type descriptor for a specific property
Mąż Zuzanny Harmider Szczęście
u mean something like this?: MainWindow.xaml
<Window>
<DataGrid ItemsSource="{Binding Source=VectorList}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="X" Binding="{Binding X}"/>
<DataGridTextColumn Header="Y" Binding="{Binding Y}"/>
<DataGridTextColumn Header="Z" Binding="{Binding Z}"/>
</DataGrid.Columns>
</DataGrid>
</Window>
<Window>
<DataGrid ItemsSource="{Binding Source=VectorList}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="X" Binding="{Binding X}"/>
<DataGridTextColumn Header="Y" Binding="{Binding Y}"/>
<DataGridTextColumn Header="Z" Binding="{Binding Z}"/>
</DataGrid.Columns>
</DataGrid>
</Window>
MainWindow.xaml.cs
namespace Project
{
public partial class MainWindow : Window,INotifyPropertyChanged
{
private ObservableCollection<Vector3> _vectors;
public ObservableCollection<Vector3> Vectors
{
get {return _vectors;}
set {
_vectors=value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName]string propertyName=""){
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName);
}
public MainWindow(){
Vectors=new ObservableCollection<Vector3>(){
//vectors here
}
}
}
}
namespace Project
{
public partial class MainWindow : Window,INotifyPropertyChanged
{
private ObservableCollection<Vector3> _vectors;
public ObservableCollection<Vector3> Vectors
{
get {return _vectors;}
set {
_vectors=value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName]string propertyName=""){
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName);
}
public MainWindow(){
Vectors=new ObservableCollection<Vector3>(){
//vectors here
}
}
}
}
Mormon Son
Mormon SonOP3w ago
Yeah but because Vector3 is fields not properties those bindings don’t work
Mąż Zuzanny Harmider Szczęście
hmm maybe create a custom class that extends Vector3 and turn them into properties
Mormon Son
Mormon SonOP3w ago
Right that’s the kinda thing I want to avoid
Mąż Zuzanny Harmider Szczęście
dont think u can do it any other way, not sure tho im not an expert or anything
Mormon Son
Mormon SonOP3w ago
It’s okay, I figured it out using “TypeProvider.AddProvider” using a TypeDescriptorProvider I wrote to pretend the fields are properties for the component model. There was a msdn magazine issue from April 2005 describing how. Required a little updating.
MODiX
MODiX3w ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?