【发布时间】:2011-05-25 18:30:44
【问题描述】:
我有一个应用模板的自定义控件。在自定义控件中,我定义了绑定到样式中的模板化控件的属性。如果我订阅了 Loaded 事件并尝试获取它们为空的属性。但是,如果我正在过度使用 OnPropertyChanged,它们就有值。有人可以解释为什么会这样。 请查看 ColumnEntity 属性。
谢谢。
我已经删除了一些简单的部分
<!-- This code is based on http://www.codeproject.com/KB/WPF/DataGridFilterLibrary.aspx -->
<Style TargetType="{x:Type local:DataGridColumnFilter}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:DataGridColumnFilter}">
<Border Background ="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<TextBox VerticalAlignment="Top" VerticalContentAlignment="Center" Background="AliceBlue"
Text="{Binding
RelativeSource={RelativeSource AncestorType={x:Type local:DataGridColumnFilter}},
Path=QueryEntity.Text, Mode=OneWayToSource, UpdateSourceTrigger=Explicit}">
</TextBox>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="{ComponentResourceKey
TypeInTargetAssembly={x:Type local:DataGridColumnFilter}, ResourceId=DataGridHeaderFilterControlStyle}"
TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<local:DataGridColumnFilter Grid.Row="0"
DataGridEntity="{Binding
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=.}"
ColumnEntity="{Binding
RelativeSource={RelativeSource AncestorType={x:Type DataGridColumnHeader}}, Path=Column}"
ItemsSourceEntity ="{Binding
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=ItemsSource}"/>
<theme:DataGridHeaderBorder Grid.Row="1"
SortDirection ="{TemplateBinding SortDirection}"
IsHovered ="{TemplateBinding IsMouseOver}"
IsPressed ="{TemplateBinding IsPressed}"
IsClickable ="{TemplateBinding CanUserSort}"
Background ="{TemplateBinding Background}"
BorderBrush ="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding ="{TemplateBinding Padding}"
SeparatorBrush ="{TemplateBinding SeparatorBrush}"
SeparatorVisibility="{TemplateBinding SeparatorVisibility}">
<TextBlock Grid.Row="1" TextWrapping="Wrap"
Text ="{TemplateBinding Content}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment ="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}">
</TextBlock>
</theme:DataGridHeaderBorder>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
public class DataGridColumnFilter : Control
{
static DataGridColumnFilter()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(DataGridColumnFilter), new FrameworkPropertyMetadata(typeof(DataGridColumnFilter)));
}
public DataGridColumnFilter()
{
this.Loaded += new RoutedEventHandler(DataGridColumnFilter_Loaded);
}
void DataGridColumnFilter_Loaded(object sender, RoutedEventArgs e)
{
// here is would be null!
var controller = ColumnEntity;
}
// For some reason this seems to be the only place to access the ColumnEntity
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
if (e.Property == ItemsSourceEntityProperty && e.OldValue != e.NewValue && null != DataGridEntity && ColumnEntity is DataGridColumn)
{
// here it works fine. The property has a proper value
var controller = ColumnEntity;
}
base.OnPropertyChanged(e);
}
#region Properties
public Query QueryEntity
{
get { return (Query)GetValue(QueryEntityProperty); }
set { SetValue(QueryEntityProperty, value); }
}
public static readonly DependencyProperty QueryEntityProperty =
DependencyProperty.Register("QueryEntity", typeof(Query), typeof(DataGridColumnFilter));
public DataGridColumn ColumnEntity
{
get { return (DataGridColumn)GetValue(ColumnEntityProperty); }
set { SetValue(ColumnEntityProperty, value); }
}
public static readonly DependencyProperty ColumnEntityProperty =
DependencyProperty.Register("ColumnEntity", typeof(DataGridColumn), typeof(DataGridColumnFilter));
public DataGrid DataGridEntity
{
get { return (DataGrid)GetValue(DataGridEntityProperty); }
set { SetValue(DataGridEntityProperty, value); }
}
public static readonly DependencyProperty DataGridEntityProperty =
DependencyProperty.Register("DataGridEntity", typeof(DataGrid), typeof(DataGridColumnFilter));
public IEnumerable ItemsSourceEntity
{
get { return (IEnumerable)GetValue(ItemsSourceEntityProperty); }
set { SetValue(ItemsSourceEntityProperty, value); }
}
public static readonly DependencyProperty ItemsSourceEntityProperty =
DependencyProperty.Register("ItemsSourceEntity", typeof(IEnumerable), typeof(DataGridColumnFilter));
#endregion
}
【问题讨论】:
标签: wpf wpf-controls binding wpfdatagrid