DataGridColumnHeader.Column上的附加属性 [英] Attached property on DataGridColumnHeader.Column
问题描述
大家好,
我有一个定义如下的附加属性:
其中FilterGrid
是覆盖的DataGrid
,而eFilterType
是在同一类中定义的Enum
.
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);
}
我在定义Column
时在Column
上设置此附加属性,如下所示:
I set this attached property on the Column
when defining the Column
as follows:
<DataGridTextColumn Header="Surname" Binding="{Binding Surname}" ctl:FilterGrid.FilterType="TextFilter">
然后,我在Generic.xaml中为FilterGrid定义了一个样式,该样式也为DataGridColumnHeader
设置了样式
我遇到的问题是找回DataGridColumnHeader.Column
上设置的FilterType附加属性.在Header的样式中,我有一个DataTrigger
,它使用如下所示的转换器:
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>
如果我从转换器返回eFilterType.TextFilter
,则可以正常工作,但是,我需要从标题的Column属性返回FilterType
附加属性.
问题在于,当我的转换器获取标头时,Column
始终为null.
我尝试在引发OnFilterTypeChanged
事件以将其设置在列上时将附加的FilterType
属性设置为标头,但是我也无法从列中找到标头,因此我尝试了给定此处,但是问题是我无法进入与列关联的DataGrid(设置它this
无效,因为它需要更改为静态方法)
我这样做是因为我不知道该如何解决,基本上我想在Column
上设置FilterType
并让Trigger
(在标题的Style
中定义)响应为基础值设置为什么
任何帮助将不胜感激.
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.
推荐答案
我找到了一种方法来做自己想做的,但不确定这是否是最好的方法.这是我最终要做的事情:
我做了一个自定义控件,该控件将在DataGridColumnHeader
中用作过滤器控件.此控件具有以下属性:
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 theDataGridColumnHeader
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);
}
当Column属性更改时,控件具有回调,然后为控件设置FilterProperty
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);
}
}
在Generic.xaml中,我将Column属性绑定到DataGridHeader的Column属性,如下所示:
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>
所有这些使我能够使用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.
这篇关于DataGridColumnHeader.Column上的附加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!