开发者

Creating custom column header in DataGrid - binding problem

开发者 https://www.devze.com 2023-04-05 03:38 出处:网络
I\'m trying to create a custom header for some of the columns in my generic DataGrid; I want these headers to include a text box which I can use to apply fiters to the data.

I'm trying to create a custom header for some of the columns in my generic 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"/>
            开发者_JS百科        </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 }

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.

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.

Creating a simple public class for storing the data and using it instead of the anonymous type solved the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号