Created
March 8, 2011 01:45
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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