I am trying to customize a DataGridColumnHeader to show multiple text fields instead of
showing only the header text provided by DataGridColumn.Header property.
If i didn't miss something, i just have to create a DataTemplate and bind to the properties
of the parent object. This works fine for the DataGridColumn.Header property but
binding to attached property fails.
For the sake of completeness, implementation of the attached property:
public static class CustomHeader
{
public static string GetUnit(DependencyObject obj) { return (string)obj.GetValue(UnitProperty); }
public static void SetUnit(DependencyObject obj, string value) { obj.SetValue(UnitProperty, value); }
public static readonly DependencyProperty UnitProperty = DependencyProperty.RegisterAttached(
"Unit", typeof(string), typeof(CustomHeader), new FrameworkPropertyMetadata(null));
}
Usage in the Xaml-Markup:
<DataGrid x:Name="tObjectDataGrid" Margin="10,50,10,10"
AutoGenerateColumns="False" EnableRowVirtualization="True"
ItemsSource="{Binding ObjectList}"
RowDetailsVisibilityMode="VisibleWhenSelected" >
<DataGrid.Resources>
<DataTemplate x:Key="CustomHeaderTemplate">
<StackPanel>
<TextBlock Text="{Binding}" />
<TextBlock Text="{Binding Path=(cust:CustomHeader.Unit)}" /> <-- attached binding doesn't work :(
</StackPanel>
</DataTemplate>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn x:Name="SpeedColumn"
Width="1*"
Binding="{Binding Speed}"
Header="Speed"
HeaderTemplate="{StaticResource CustomHeaderTemplate}"
cust:CustomHeader.Unit="[m/s]" />
</DataGrid.Columns>
</DataGrid>
I really appreciate any comment or weblink that clarifies what i am missing here. Thanks in advance.
You should use multi value converter (msdn).
XAML:
Code-behind:
I think that better solution will be create new class e.g
HeaderData
and after that you can create instance of it in xaml and binding to this class.Example:
Class to hold header data:
XAML code: