Skip to content

Instantly share code, notes, and snippets.

@jgable
Created March 8, 2011 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgable/859686 to your computer and use it in GitHub Desktop.
Save jgable/859686 to your computer and use it in GitHub Desktop.
A simple behavior for adding a bindable column header to a DataGridTextColumn (or any other DataGridColumn)
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
public class BindableColumnHeader : Behavior<DataGridColumn>
{
/// <summary>
/// The <see cref="Header" /> dependency property's name.
/// </summary>
public const string HeaderPropertyName = "Header";
/// <summary>
/// Gets or sets the value of the <see cref="Header" />
/// property. This is a dependency property.
/// </summary>
public object Header
{
get
{
return (object)GetValue(HeaderProperty);
}
set
{
SetValue(HeaderProperty, value);
}
}
/// <summary>
/// Identifies the <see cref="Header" /> dependency property.
/// </summary>
public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register(
HeaderPropertyName,
typeof(object),
typeof(BindableColumnHeader),
new PropertyMetadata(new PropertyChangedCallback(HandleHeaderBindingChanged)));
private static void HandleHeaderBindingChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
var behave = obj as BindableColumnHeader;
if (behave == null || behave.AssociatedObject == null)
return;
behave.AssociatedObject.Header = args.NewValue;
}
protected override void OnAttached()
{
if (this.AssociatedObject == null)
return;
this.AssociatedObject.Header = this.Header;
base.OnAttached();
}
}
<sdk:DataGridTextColumn
Width="2*"
Binding="{Binding Name}">
<i:Interaction.Behaviors>
<controls:BindableColumnHeader
Header="{Binding Path=NameHeader, Source={StaticResource LocalizationManager}}" />
</i:Interaction.Behaviors>
</sdk:DataGridTextColumn>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment