0

I have a control that is wrapping an Xceed DataGridControl (part of the Extended WPF Toolkit Community Edition). The control provides a simple property (without a backing dependency property) that can hold a list of buttons (field instantiated by the constructor):

public List<Button> GroupButtons
{
    get { return groupButtons; }
    set { groupButtons = value; }
}

The items of the property are then added in the XAML of a view that is using the control:

<local:CustomControl ...>

    <local:CustomControl.GroupButtons>
        <Button>foo<Button>
    </local:CustomControl.GroupButtons>

    ...
</local:CustomControl ...>

I would like to render the buttons of this list inside the so-called "GroupHeaderControl" of the Xceed Datagrid, which is basically a grouping row like shown below:

enter image description here

To achieve this, I've overwritten the ControlTemplate of the GroupHeaderControl:

<ResourceDictionary ...>
    <Style TargetType="{x:Type controls:CustomControl}">
        <Style.Resources>
            <Style TargetType="{x:Type xcdg:GroupHeaderControl}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type xcdg:GroupHeaderControl}">
                            <Border ...>
                                <StackPanel Height="{TemplateBinding Height}" Orientation="Horizontal"> 

                                    <ContentPresenter  />                        
                                    <ItemsControl ItemsSource="{Binding GroupButtons,  RelativeSource={RelativeSource AncestorType={x:Type controls:CustomControl}}}" />

                                </StackPanel>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Style.Resources>
    </Style>
    ...
</ResourceDictionary>

Now here comes the problem: Instead of rendering the button(s) for each instance of the GroupHeaderControl, it is rendered only once. For illustration, imagine that in the image above only the button at the second group header ("Lyon") is visible while the other one ("Reims") is not.

The problem is apparently related to the fact that the items of the GroupButtons list are added via the XAML definition. If I hard code the items of the list, it works like a charm:

public List<Button> ButtonList
{
    get { return new List<Button>()
    {
        new Button() { Content = "foo" }
    }
}

I don't really get where this behavior is coming from. Does somebody have an idea?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Daniel Lemke
  • 1,966
  • 18
  • 24
  • 1
    Try adding the buttons in the template instead of binding to your UserControl. The UserControl and therefore the buttons exist only once in your application while the template gets applied multiple times. Since visuals can only exist once in the visual tree it is only added to one header. In your code approach the getter builds new instances of the button list each time it is being accessed which is why this approach works. – David Jan 26 '16 at 14:48
  • Hi David, thanks for your hints. You were right, the actual problem here is that the button is a FrameworkElement, which always has exactly one parent in the tree. Adding the buttons in the template won't be possible in my scenario as the template is strictly separated from "users" of the control, but that's a different story. If you post your comment as answer, I'll mark it. – Daniel Lemke Jan 27 '16 at 12:30
  • You can take a list of group button templates if you want, so what's keeping you from adding the buttons in the template? – grek40 Jan 23 '17 at 13:01

0 Answers0