WPF Expander 在 DataGrid 中的使用(代码隐藏)

发布于 2025-01-16 08:59:17 字数 3443 浏览 3 评论 0原文

我试图理解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文