Skip to content

Instantly share code, notes, and snippets.

@Martin-Andersen
Created September 11, 2014 07:28
Show Gist options
  • Save Martin-Andersen/d801bde4987ef2ce1bec to your computer and use it in GitHub Desktop.
Save Martin-Andersen/d801bde4987ef2ce1bec to your computer and use it in GitHub Desktop.
This is a WPF DataGridTextColumn with some very common properties that should have been build in from the start (-: You get TextTrimming, HorizontalAlignment and ToolTip
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace FirstFloor.ModernUI.Windows.Controls
{
/// <summary>
/// A DataGrid text column using default Modern UI element styles.
/// </summary>
public class DataGridTextColumn : System.Windows.Controls.DataGridTextColumn
{
private BindingBase _toolTip;
public DataGridTextColumn()
{
ElementStyle = Application.Current.Resources["DataGridTextStyle"] as Style;
EditingElementStyle = Application.Current.Resources["DataGridEditingTextStyle"] as Style;
}
/// <summary>
/// The tooltip binding that will be applied to the generated element.
/// </summary>
/// <remarks>
/// This isn't a DP because if it were getting the value would evaluate the binding.
/// </remarks>
public BindingBase ToolTip
{
get { return _toolTip; }
set
{
if (_toolTip == value) return;
_toolTip = value;
NotifyPropertyChanged("ToolTip");
}
}
public static readonly DependencyProperty TextTrimmingProperty = DependencyProperty.Register(
"TextTrimming", typeof (TextTrimming), typeof (DataGridTextColumn),
new PropertyMetadata(default(TextTrimming)));
/// <summary>
/// HorizontalAlignment Dependency Property.
/// </summary>
public static readonly DependencyProperty HorizontalAlignmentProperty =
DependencyProperty.Register(
"HorizontalAlignment",
typeof (HorizontalAlignment),
typeof (DataGridTextColumn),
new FrameworkPropertyMetadata(HorizontalAlignment.Left, FrameworkPropertyMetadataOptions.AffectsArrange), ValidateHorizontalAlignmentValue);
/// <summary>
/// TextTrimming, only works in read-only mode
/// </summary>
public TextTrimming TextTrimming
{
get { return (TextTrimming) GetValue(TextTrimmingProperty); }
set { SetValue(TextTrimmingProperty, value); }
}
/// <summary>
/// HorizontalAlignment Property.
/// </summary>
public HorizontalAlignment HorizontalAlignment
{
get { return (HorizontalAlignment) GetValue(HorizontalAlignmentProperty); }
set { SetValue(HorizontalAlignmentProperty, value); }
}
internal static bool ValidateHorizontalAlignmentValue(object value)
{
var ha = (HorizontalAlignment) value;
return (ha == HorizontalAlignment.Left
|| ha == HorizontalAlignment.Center
|| ha == HorizontalAlignment.Right
|| ha == HorizontalAlignment.Stretch);
}
protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
{
var element = (TextBlock) base.GenerateElement(cell, dataItem);
element.TextTrimming = TextTrimming;
element.HorizontalAlignment = HorizontalAlignment;
ApplyToolTipBinding(element, FrameworkElement.ToolTipProperty);
return element;
}
// Copied from DataGridTextColumn because it's not protected there either. Seems like it should be.
internal void ApplyToolTipBinding(DependencyObject target, DependencyProperty property)
{
var binding = _toolTip;
if (binding == null)
BindingOperations.ClearBinding(target, property);
else
BindingOperations.SetBinding(target, property, binding);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment