Possible Duplicate:
Wpf Toolkit. Bind DataGrid Column Header to DynamicResource
WPF Error: Cannot find govering FrameworkElement for target element
I am creating a WPF application using the MVVM pattern, so on my view I am trying to bind the column header of a DataGrid column to a property on my view model which is the data context of the view that the DataGrid is in.
XAML:
<DataGrid Name="DailyData" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" ItemsSource="{Binding Path=DailyDataViewModels}" HorizontalAlignment="Stretch">
<DataGrid.Columns>
<DataGridTextColumn Header="{Binding InflowVolumeLabel}" Binding="{Binding InflowVolume}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
However the header just shows up blank and does not seem to be trying to bind to the specified property, how can you bind a DataGrid column header?
You’ve run into a neat problem with the
DataGrid.Columnscollection, namely they are not a member of the same visual tree and thus only theBindingproperty appears to work.The only approach I’ve found that works is to add an attached property to the
DataGridwhich will apply the headers after the data is loaded.Potentially used like (could be improved):