相关文章推荐
瘦瘦的烤红薯  ·  SAML 要求 - Tableau·  3 月前    · 
善良的回锅肉  ·  python pillow ...·  1 年前    · 
威武的豌豆  ·  在Visual ...·  1 年前    · 
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" / > < /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. 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:
< 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.
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •