从TextBox开始获取GridViewColumn标头

时间:2014-08-07 14:57:35

标签: c# wpf

                        <GridViewColumn Header="#ErrorRepeat1" Width="100">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBox Text="{Binding NoErrorRepeat1}"
                                             TextAlignment="Center"
                                             IsReadOnly="True"
                                             Background="{Binding IsPresentGroup1, Converter={StaticResource BoolToColorConverter}}"                                                                                          >
                                        <TextBox.ContextMenu>
                                            <ContextMenu>
                                                <MenuItem Header="Highlight Activity" Click="menuActivityFocus"/>
                                            </ContextMenu>
                                        </TextBox.ContextMenu>
                                    </TextBox>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>

我正在尝试使用:

从TextBox开始获取GridViewColumn标头
 MenuItem mi = sender as MenuItem;
        var menu = mi.Parent as ContextMenu;
        if (menu != null)
        {
            TextBox tb = menu.PlacementTarget as TextBox;
            var gv = FindVisualParent<GridViewColumn>(tb);
        }

但这种方法没有成功。

我需要提示如何访问GridViewColumn以获取标题内容。

1 个答案:

答案 0 :(得分:0)

您可以将值绑定到Header属性,然后从后面的代码访问数据绑定属性。尝试这样的事情:

<GridViewColumn Header="{Binding ColumnHeader}" Width="100">
    ...
</GridViewColumn>

...

MenuItem mi = sender as MenuItem;
var menu = mi.Parent as ContextMenu;
if (menu != null)
{
    string columnHeader = ColumnHeader;
    ...
}