Click here to Skip to main content
11,926,933 members (54,463 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# XAML WPF DataGrid
Hi all,

I have an attached property defined as follows:
Where FilterGrid is an overridden DataGrid and eFilterType is an Enum defined in the same class.

public static readonly DependencyProperty FilterTypeProperty =
    DependencyProperty.RegisterAttached("FilterType", typeof(eFilterType), typeof(FilterGrid), new PropertyMetadata((eFilterType)(-1),new PropertyChangedCallback(OnFilterTypeChanged)));
    public static eFilterType GetFilterType(DependencyObject control)
      return (eFilterType)control.GetValue(FilterTypeProperty);
    public static void SetFilterType(DependencyObject control, eFilterType Value)
      control.SetValue(FilterTypeProperty, Value);

I set this attached property on the Column when defining the Column as follows:

<DataGridTextColumn Header="Surname" Binding="{Binding Surname}" ctl:FilterGrid.FilterType="TextFilter">

I then have a style defined in Generic.xaml for the FilterGrid which also sets a style for the DataGridColumnHeader

The problem I'm having is getting back the FilterType attached property that was set on the DataGridColumnHeader.Column. In the Header's style I have a DataTrigger that uses a converter as follows:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},Converter={StaticResource HeaderToFilterTypeConverter}}" Value="TextFilter">
<Setter Property="Background" Value="Blue"/>

If I return eFilterType.TextFilter from my converter it works fine, however, I need to return the FilterType attached property from the header's Column property.

The problem is that Column is always null when my converter gets the header.

I have tried setting the attached FilterType property to the header when the OnFilterTypeChanged event is raised for setting it on the column, but I cannot find the header from the Column either, I did try the solution given here, but the problem there is that I cannot get to the DataGrid associated with the Column (setting it to this doesn't work as it needs to change to a static method)

I am doing it this way as I do not know how else to aproach this, basically I want to set a FilterType on the Column and have a Trigger (defined in the Style for the header) respond based on what the value was set to

Any help would be appreciated.
Posted 4-Oct-11 22:23pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I found a way to do what I wanted, not sure if it's the best way though. Here is what I ended up doing:

I made a custom control that will be used in the DataGridColumnHeader as a filter control. This control has the following properties:

/// <summary>
/// Filter type for each instance of the FilterControl
/// </summary>
public static readonly DependencyProperty FilterProperty = DependencyProperty.Register("Filter", typeof(eFilterType), typeof(FilterControl),new PropertyMetadata((eFilterType)(-1)));
public eFilterType Filter
  get { return (eFilterType)GetValue(FilterProperty); }
  set { SetValue(FilterProperty, value); }
/// <summary>
/// Column property used to get to the FilterType attached property to be able to set the FilterProperty
/// </summary>
public static readonly DependencyProperty ColumnProperty = DependencyProperty.Register("Column", typeof(DataGridColumn), typeof(FilterControl), new PropertyMetadata(null,new PropertyChangedCallback(ColumnChanged)));
public DataGridColumn Column
  get { return (DataGridColumn)GetValue(ColumnProperty); }
  set { SetValue(ColumnProperty, value); }
/// <summary>
/// FilterType attached property - used on column definition from the outside application
/// </summary>
public static readonly DependencyProperty FilterTypeProperty = DependencyProperty.RegisterAttached("FilterType", typeof(eFilterType), typeof(FilterControl), new PropertyMetadata((eFilterType)(-1)));
public static eFilterType GetFilterType(DataGridColumn Column)
  return (eFilterType)Column.GetValue(FilterTypeProperty);
public static void SetFilterType(DataGridColumn Column,eFilterType Type)
  Column.SetValue(FilterTypeProperty, Type);

The control has a callback when the Column property changes, it then sets the FilterProperty for the control
public static void ColumnChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
     if (e.NewValue is DataGridColumn)
       DataGridColumn Col = (DataGridColumn)e.NewValue;
       eFilterType Type = (eFilterType)Col.GetValue(FilterTypeProperty);
       o.SetValue(FilterProperty, Type);

In the Generic.xaml I bound the Column property to the DataGridHeader's Column property like this:
<local:filtercontrol column="{Binding <br mode=" hold=" />                              RelativeSource={RelativeSource <br mode=" xmlns:local="#unknown"></local:filtercontrol>

All this enables me to use a Trigger from the FilterControl's template like this:
                        <trigger property="Filter" value="BoolFilter">
                            <setter property="Background" value="Red">

That is what I have currently, please let me know if there is a better way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 5 Oct 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100