Click here to Skip to main content
15,794,275 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am generating expanders via code (not xaml) and i would like to have the header contain a stackpanel. I know this is possible as i have allready done it in xaml. I followed the following msdn tutorial:"The article" . I then inserted a stackpanel in the bulletdecorator.

But instead of displaying the contents of the bulletdecorator my expander header is showing "System.Windows.Controls.Primitives.BulletDecorator". How can i achieve this? At the end my expander header should contain some text and a datagrid with 1 row of data (the sum of the underlying datarows)

here is a code snippet:
//Create containing stack panel and assign to Grid row/col
            Grid.SetRow(ParentPanel, 0);
            Grid.SetColumn(ParentPanel, 1);
            StackPanel InnerSTack = new StackPanel();
            //Create Expander object
            Expander exp = new Expander();
            exp.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            exp.IsExpanded = true;
            exp.Width = ParentPanel.Width;
            //and the bulletdecorator with the datagrid 
            System.Windows.Controls.Primitives.BulletDecorator bp = new System.Windows.Controls.Primitives.BulletDecorator();
            Image im = new Image();
            BitmapImage bi = new BitmapImage();
            bi.UriSource = new Uri(@"pack://application:,,/images/icon.jpg");
            im.Source = bi;
            im.Width = 10;
            bp.Bullet = im;
                TextBlock tb = new TextBlock();
                tb.Text = TN.Text;
                tb.Margin = new Thickness(20,0,0,0);
                bp.Child =  InnerSTack;
             exp.Header = bp;

1 solution

Go through this article -[^].

var expander = sender as Expander;
    this.Title = expander.Header.ToString();

While this does not answer your question completed, what you can do is capture the sender.
Then get the first child of that header using GetVisualChild[^].
Once you have the header control, add the stack panel.
Share this answer
ksanamur 20-Feb-15 5:00am    
Thanks for the suggestion but this is not working.
I allready have the expander header object in the code (i generate it myself)
but when i attach the stack panel to it it is not being shown in the header but it's converted to text

How do i prevent the stackpanel or any object in the header to be converted to text ?
Abhinav S 20-Feb-15 6:04am    
Can you go through this -
Its exactly what you need to do but via code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900