Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
35
How to get current style
posted

I want to style the header for another control in my app to the same header that's used by the XamDataGrid. The XamDataGrid's header can change depending on whether my app is running under Windows XP or Vista so I don't think I can just hardcode a style.

I've used Snoop and found that the header is an igWindows:CardPanel with a name of SpacerFill. But at this point I'm kind of lost on how to actually get at that object. Smile Any help would be appreciated.

Parents
No Data
Reply
  • 35
    Verified Answer
    posted

    I figured out a way to do this. It involves more code than I thought I'd need, but it works. I ended up having to recreate the XAML structure myself and had code like this:

    <Grid Name="headerGrid">
      <Rectangle />
      <Rectangle Height="1" VerticalAlignment="Top" Margin="0,1,0,0" />
      <Rectangle Height="1" VerticalAlignment="Top" />
      <TextBlock Padding="4">Event Search</TextBlock>
    </
    Grid>

    After that I had to use some C# code to walk the visual tree of the document until I found the SpacerFill element in the XamDataGrid.

    private DependencyObject WalkTree(DependencyObject current, Predicate<DependencyObject> match) {
      if(match.Invoke(current))
        return current;

      DependencyObject target;
     
    int childCount = VisualTreeHelper.GetChildrenCount(current);

      for(int i = 0; i < childCount; i++) {
       
    target = this.WalkTree(VisualTreeHelper.GetChild(current, i), match);
       
    if(target != null)
         
    return target;
     
    }

      return null;
    }

    Once I found it I manually assigned the brushes to my XAML:

    private void Window_Loaded(object sender, RoutedEventArgs e) {
     
    Panel panel = this.WalkTree(this.xamDataGrid1, o => o.GetValue(NameProperty).Equals("SpacerFill")) as Panel;

      if(panel != null) {
       
    ((Rectangle) this.headerGrid.Children[0]).Fill = ((Rectangle) panel.Children[0]).Fill;
       
    ((Rectangle) this.headerGrid.Children[1]).Fill = ((Rectangle) panel.Children[1]).Fill;
       
    ((Rectangle) this.headerGrid.Children[2]).Fill = ((Rectangle) panel.Children[2]).Fill;
     
    }
    }

    BTW, I'm using Visual Studio 2008 so I could use lambda expressions from C# 3.0 in the first line of my Window_Loaded method. Helps allow the WalkTree method to be more reusable.

Children
No Data