【问题标题】:Creating custom column header in DataGrid - binding problem在 DataGrid 中创建自定义列标题 - 绑定问题
【发布时间】:2011-11-18 16:26:47
【问题描述】:

我正在尝试为我的通用 DataGrid 中的某些列创建自定义标题;我希望这些标题包含一个文本框,我可以使用该文本框将过滤器应用于数据。

这是我目前所拥有的:

    <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>

这是我目前正在玩弄的标题所使用的样式。这是在创建时为标头提供适当数据的代码生成:

    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;
         (...)
    }

运行应用程序时,列的 TextBlock 将包含以下内容:{ Text = Description, Tag = Description }

因此,我希望绑定的 Path=Tag 部分能够正常工作,但是,当TextChanged 事件被触发时,标签是 null

我做错了什么?

【问题讨论】:

    标签: c# silverlight-4.0 datagrid


    【解决方案1】:

    据我所知,使用匿名类型显然不适用于 XAML 和绑定...这很奇怪,因为它使用反射。

    创建一个简单的公共类来存储数据并使用它而不是匿名类型解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      • 2011-12-25
      • 1970-01-01
      相关资源
      最近更新 更多