Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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:

HTML
<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:

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


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

1 solution

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:

C#
/// <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:
HTML
<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:
HTML
<controltemplate.triggers>
                        <trigger property="Filter" value="BoolFilter">
                            <setter property="Background" value="Red">
                                    TargetName="tbText"/>
                        </setter></trigger></controltemplate.triggers>


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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900