从标头绑定到依赖属性 [英] Binding from header to dependency property
本文介绍了从标头绑定到依赖属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好
我想更改我的DataGrid列标题本身有两个文本框。我创建了一个类和Depndency属性:myProperty但绑定不起作用。我尝试了不同的组合,不幸的是我不知道我在哪里做错了。请帮助
我的尝试:
栏目:
Hello
I would like to change my DataGrid column headings have two text boxes in itself. I created a class and Depndency property: myProperty but binding not work. I tried different combinations, unfortunately I do not know where I'm doing a mistake. Please help
What I have tried:
Column:
<extensions:CustomDataGridTextColumn HeaderStyle="{StaticResource Nagłówek2tb}" Header="Nagłówek 1" MyProperty ="Nagłówek2"/>
Style:
<Style TargetType="{x:Type DataGridColumnHeader}" x:Key="Nagłówek2tb">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate >
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding}" TextWrapping="Wrap"/>
<TextBlock Grid.Row="1" Text="{Binding MyProperty, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridTextColumn}}"/>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
Class
Class
public class CustomDataGridTextColumn : System.Windows.Controls.DataGridTextColumn
{
public string MyProperty
{
get { return (string)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(string), typeof(CustomDataGridTextColumn), new UIPropertyMetadata(""));
}
推荐答案
简答: {Binding}不是捷径用于绑定到自身(在RelativeSource.Self的意义上)。相反,{Binding}等同于{Binding Path =。},它绑定到当前源。
Short answer: {Binding} is not a shortcut for "binding to itself" (in the sense of RelativeSource.Self). Rather, {Binding} is equivalent to {Binding Path=.}, which binds to the current source.
DataGridColumnHeader
是一个可怕的野兽,它既不是视觉树也不是逻辑树的一部分。你需要一个BindingProxy
,还有很多工作要做。参见例如
[WPF]如何在未继承DataContext的情况下绑定到数据»Thomas Levesque的.NET博客 [ ^ ]
DataGridColumnHeader
is a terrible beast, it is neither part of the Visual Tree nor Logical Tree. You need aBindingProxy
, and a lot of work around. See e.g.
[WPF] How to bind to data when the DataContext is not inherited » Thomas Levesque's .NET blog[^]
这篇关于从标头绑定到依赖属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文