WPF Expander usage inside DataGrid with code behind

742 views Asked by At

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);
0

There are 0 answers