none
如何通过 DataGridColumnHeader header 获取到里面的元素? RRS feed

  • 问题

  • 例如:

        <toolkit:DataGridTemplateColumn ToolTipService.ToolTip="{Binding Path=Description}">
                            
                                <toolkit:DataGridTemplateColumn.HeaderTemplate>
                                <DataTemplate x:Uid="fff">
                                    <StackPanel>
                                        <TextBlock Text="事件描述"  Uid="sss"  ></TextBlock>
                                    </StackPanel>
                                </DataTemplate>
                            </toolkit:DataGridTemplateColumn.HeaderTemplate>
                            <toolkit:DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock TextTrimming="WordEllipsis" Tag="cc" Uid="uu"  ToolTipService.ToolTip="{Binding Description}" Width="Auto" Margin="5">
                                     <TextBlock.Text>
                                        <Binding Path="Description"> 
                                            <Binding.Converter>
                                                 <cvt:ItemDescriptionConverter />   
                                            </Binding.Converter>
                                        </Binding> 
                                     </TextBlock.Text>
                                        
                                    </TextBlock>
    
                                </DataTemplate>
                            </toolkit:DataGridTemplateColumn.CellTemplate> 
                                
                                <!--<Binding Path="Description">
                                    
                                    <Binding.Converter>
                                   <cvt:ItemDescriptionConverter />     
                                    </Binding.Converter>
                                </Binding>-->
                            
                               
                        </toolkit:DataGridTemplateColumn>
    

    代码里面怎么得到header里面的元素呢?根据header这个对象。

    2011年9月20日 10:10

答案

  • 你可以通过遍历VisualTree去得到控件, 比如(FindControlByName,FindControlByType):

    public static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            string controlName = child.GetValue(Control.NameProperty) as string;
            if (controlName == name)
            {
                return child as T;
            }
            else
            {
                T result = FindVisualChildByName<T>(child, name);
                if (result != null)
                    return result;
            }
        }
        return null;
    }
    

     

    public DependencyObject FindChild(DependencyObject o, Type childType)
            {
                DependencyObject foundChild = null;
                if (o != null)
                {
                    int childrenCount = VisualTreeHelper.GetChildrenCount(o);
                    for (int i = 0; i < childrenCount; i++)
                    {
                        var child = VisualTreeHelper.GetChild(o, i);
                        if (child.GetType() != childType)
                        {
                            foundChild = FindChild(child, childType);
                        }
                        else
                        {
                            foundChild = child;
                            break;
                        }
                    }
                }
                return foundChild;
            }
    

     

     


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 a013strife 2011年9月26日 2:49
    2011年9月21日 8:33
    版主