WPF Expander 在 DataGrid 中的使用(代码隐藏)
我试图理解 DataGrid 内 Expander 控件中 XAML 背后的逻辑。首先,我在 DataGrid 内的 XAML 中创建了这段代码,它工作正常。
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander>
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Magazyn:" Margin="5" />
<TextBlock Text="{Binding Path=Name}" Margin="5" FontWeight="Bold" Foreground="Blue" />
<TextBlock Text="Liczba produktów:" Margin="5"/>
<TextBlock Text="{Binding Path=ItemCount}" Margin="5" FontWeight="Bold" Foreground="Blue"/>
</StackPanel>
</Expander.Header>
<ItemsPresenter/>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
然后我创建了它的代码隐藏版本,除了 Expander 的标头之外,它几乎可以工作,如果我在 SetValue 方法字符串中使用它作为第二个参数,它就可以工作。但是,如果我像 XAML 中那样使用 StackPanel 实例,则会出现异常。其原因何在。为什么 StackPanel 被允许作为 XAML 中的标头,并且它不在代码隐藏中?
var groupStyle = new GroupStyle();
var style = new Style(typeof(GroupItem));
var setter = new Setter();
var template = new ControlTemplate(typeof(GroupItem));
var stackPanel = new StackPanel();
stackPanel.Orientation = Orientation.Horizontal;
stackPanel.Children.Add(new TextBlock() { Text = "Magazyn:", Margin = new Thickness(5) });
var tb = new TextBlock();
tb.Margin = new Thickness(5);
tb.FontWeight = FontWeights.Bold;
tb.Foreground = Blue;
tb.SetBinding(TextBlock.TextProperty, new Binding("Name"));
stackPanel.Children.Add(tb);
stackPanel.Children.Add(new TextBlock() { Text = "Liczba produktów:", Margin = new Thickness(5) });
tb = new TextBlock();
tb.Margin = new Thickness(5);
tb.FontWeight = FontWeights.Bold;
tb.Foreground = Blue;
tb.SetBinding(TextBlock.TextProperty, new Binding("ItemCount"));
stackPanel.Children.Add(tb);
template.VisualTree = new FrameworkElementFactory(typeof(Expander));
template.VisualTree.SetValue(Expander.HeaderTemplateProperty, stackPanel);
template.VisualTree.AppendChild(new FrameworkElementFactory(typeof(ItemsPresenter)));
setter.Property = TemplateProperty;
setter.Value = template;
style.Setters.Add(setter);
groupStyle.ContainerStyle = style;
groupStyle.ContainerStyle.Setters.Add(setter);
gridProdukty.GroupStyle.Add(groupStyle);
I am trying to understand logic behind XAML in Expander control inside DataGrid. First I created this code in XAML inside the DataGrid and it works fine.
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander>
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Magazyn:" Margin="5" />
<TextBlock Text="{Binding Path=Name}" Margin="5" FontWeight="Bold" Foreground="Blue" />
<TextBlock Text="Liczba produktów:" Margin="5"/>
<TextBlock Text="{Binding Path=ItemCount}" Margin="5" FontWeight="Bold" Foreground="Blue"/>
</StackPanel>
</Expander.Header>
<ItemsPresenter/>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
Then I created it's code behind version and it almost works except of Expander's Header, which works if I use in SetValue method string as the second argument. But if I use a StackPanel instance as in XAML I get an exception. What is the reason of it. Why is StackPanel allowed as Header in XAML and it's not in code behind?
var groupStyle = new GroupStyle();
var style = new Style(typeof(GroupItem));
var setter = new Setter();
var template = new ControlTemplate(typeof(GroupItem));
var stackPanel = new StackPanel();
stackPanel.Orientation = Orientation.Horizontal;
stackPanel.Children.Add(new TextBlock() { Text = "Magazyn:", Margin = new Thickness(5) });
var tb = new TextBlock();
tb.Margin = new Thickness(5);
tb.FontWeight = FontWeights.Bold;
tb.Foreground = Blue;
tb.SetBinding(TextBlock.TextProperty, new Binding("Name"));
stackPanel.Children.Add(tb);
stackPanel.Children.Add(new TextBlock() { Text = "Liczba produktów:", Margin = new Thickness(5) });
tb = new TextBlock();
tb.Margin = new Thickness(5);
tb.FontWeight = FontWeights.Bold;
tb.Foreground = Blue;
tb.SetBinding(TextBlock.TextProperty, new Binding("ItemCount"));
stackPanel.Children.Add(tb);
template.VisualTree = new FrameworkElementFactory(typeof(Expander));
template.VisualTree.SetValue(Expander.HeaderTemplateProperty, stackPanel);
template.VisualTree.AppendChild(new FrameworkElementFactory(typeof(ItemsPresenter)));
setter.Property = TemplateProperty;
setter.Value = template;
style.Setters.Add(setter);
groupStyle.ContainerStyle = style;
groupStyle.ContainerStyle.Setters.Add(setter);
gridProdukty.GroupStyle.Add(groupStyle);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论