问题描述
我通过代码(而不是xaml)生成扩展器,我希望头部包含一个stackpanel。我知道这是可能的,因为我已经在xaml中完成了它。我按照以下msdn教程:。然后我在bulletdenel中插入了一个stackpanel。
但是我没有显示bulletdecorator的内容,而是我的扩展器标题显示System.Windows.Controls.Primitives.BulletDecorator 。我怎样才能实现这一目标?最后,我的扩展器标题应该包含一些文本和一行数据(包含1行数据)(基础数据行的总和)
这里是一个代码片段:
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);
InnerSTack.Children.Add(tb);
bp.Child = InnerSTack;
exp.Header = bp;
推荐答案
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.
这篇关于如何通过代码将Stackpanel添加到扩展器头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!