C
C#7mo ago
yambo

WPF binding problem in datagrid header

Hi, so I have set up a viewmodel and a view. The viewmodel is referenced inside the view's XAML as Page.DataContext. The viewmodel has INotifyPropertyChanged implemented and the necessary properties are public and have a getter and are set to a string value. Now when I am binding the value to the Header property of the datagrid column ("DataGridTextColumn"), it does not show up. However the property "Binding" where a property of the viewmodel's list of persons is bound, it works perfectly. By debugging I found this message: Severity Count "Cannot find governing FrameworkElement or FrameworkContentElement for target element." Data Context: null I have already tried setting the DataContext via the relative source, changing merely anything i could think of, but im stumped. Please help me
No description
No description
No description
13 Replies
yambo
yamboOP7mo ago
This is .NET 8.0 and VS2022
leowest
leowest7mo ago
No description
leowest
leowest7mo ago
you dont need this ObservableCollection already does it by default u can just
public ObservableCollection<PersonModel> Persons {get;set;}
public ObservableCollection<PersonModel> Persons {get;set;}
leowest
leowest7mo ago
u also dont need to do
No description
leowest
leowest7mo ago
you are already initializing it to something as for the header I am unsure maybe its trying to find it from the Persons collection try doing DataContext.IsMarriedHeader I will check in a minute just go home @yambo mind $paste to the link below your vm and xaml so I dont have to rewrite it
MODiX
MODiX7mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
yambo
yamboOP7mo ago
BlazeBin - zlbimdiolink
A tool for sharing your source code with the world!
yambo
yamboOP7mo ago
BlazeBin - jmqkiyavdcfe
A tool for sharing your source code with the world!
yambo
yamboOP7mo ago
Thank you! yeah this was just one of my desperate attempts :D yeah this doesnt work for me sadly, i already tried a lot of combinations
leowest
leowest7mo ago
and thx for the pastes I will take a look in a few its a bit more complex than I expected because it doesn't let u relative from the Header apparently...
<DataGridTextColumn Binding="{Binding FirstName}">
<DataGridTextColumn.Header>
<TextBlock Text="{Binding DataContext.HeaderName, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding FirstName}">
<DataGridTextColumn.Header>
<TextBlock Text="{Binding DataContext.HeaderName, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
leowest
leowest7mo ago
No description
yambo
yamboOP7mo ago
goddamn thank you so much may i ask you what your thought process was behind fixing this? i swear i tried out so much with relativesource binding
leowest
leowest7mo ago
I just looked into the source saw it had a template and tried to bind to it since it couldn't reference a path or source from the Binding that would let me use an outer context
Want results from more Discord servers?
Add your server