[英]Creating custom column header in DataGrid - binding problem
I'm trying to create a custom header for some of the columns in my generic DataGrid; 我正在尝试为通用DataGrid中的某些列创建一个自定义标头。 I want these headers to include a text box which I can use to apply fiters to the data.
我希望这些标题包含一个文本框,可用于将拟合器应用于数据。
This is what I have so far: 这是我到目前为止的内容:
<Style x:Key="ColumnHeaderStyle" TargetType="dataprimitives:DataGridColumnHeader">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding}"/>
<TextBox Width="Auto" Tag="{Binding Path=Tag}" LostFocus="TextBox_LostFocus" TextChanged="TextBox_TextChanged" MinWidth="50"/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
This is the style used by the header I'm toying with at the moment. 这是我目前正在使用的标头使用的样式。 Here's the code generation which feeds the header the appropriate data on creation:
这是代码生成,它在创建时向标题提供适当的数据:
private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
(...)
e.Column.Header = new { Text = e.Column.Header, Tag = e.PropertyName };
e.Column.HeaderStyle = Resources["ColumnHeaderStyle"] as Style;
(...)
}
When the application is ran, the TextBlock of a column will contains the following: { Text = Description, Tag = Description }
运行应用程序时,列的TextBlock将包含以下内容:
{ Text = Description, Tag = Description }
As such, I'd expect the Path=Tag
part of the binding to work, however, when the TextChanged
event is hit, the tag is null
. 因此,我希望绑定的
Path=Tag
部分起作用,但是,当命中TextChanged
事件时,标记为null
。
What am I doing wrong? 我究竟做错了什么?
Apparently using anonymous types doesn't work well with XAML and binding... which is odd, as it uses reflection, as far as I know. 显然,使用匿名类型不能与XAML和绑定一起很好地工作……据我所知,这很奇怪,因为它使用了反射。
Creating a simple public class for storing the data and using it instead of the anonymous type solved the problem. 创建一个简单的公共类来存储数据并使用它代替匿名类型可以解决该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.