How to style items in the ItemsPresenter of a custom ItemsControl?

1.9k views Asked by At

I am trying to create a custom ItemsControl, which shows RadioButtons or Checkboxes and applies styles to those items automatically.

This is what I have right now:

Custom ItemsControl:

public class LabelContainer : ItemsControl
{
    public string Label
    {
        get { return (String)GetValue(LabelProperty); }
        set { SetValue(LabelProperty, value); }
    }

    public static readonly DependencyProperty LabelProperty =
 DependencyProperty.Register("Label", typeof(string),
   typeof(LabelContainer), new PropertyMetadata(""));
}

Styles:

<ItemsPanelTemplate x:Key="ItemsPanelTemplate">
    <StackPanel Orientation="Horizontal">
        <StackPanel.Resources>
            <Style TargetType="{x:Type RadioButton}" >
                <Setter Property="Margin" Value="0,0,8,0"/>
            </Style>
        </StackPanel.Resources>
    </StackPanel>
</ItemsPanelTemplate>

<Style TargetType="local:LabelContainer">
    <Setter Property="ItemsPanel" Value="{StaticResource ItemsPanelTemplate}"></Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:LabelContainer">
                <StackPanel>
                    <TextBlock FontWeight="Medium" Margin="0,0,7,6" Text="{TemplateBinding Label}"/>
                    <ItemsPresenter>
                        <ItemsPresenter.Resources>
                            <Style TargetType="{x:Type RadioButton}">
                                <Setter Property="Margin" Value="0,0,8,0"/>
                            </Style>
                            <Style TargetType="{x:Type CheckBox}">
                                <Setter Property="Margin" Value="0,0,8,0"/>
                            </Style>
                        </ItemsPresenter.Resources>
                    </ItemsPresenter>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And I am using it like this:

<local:LabelContainer Label="RadioButtons:">
    <RadioButton>Nr 1</RadioButton>
    <RadioButton>Nr 2</RadioButton>
</local:LabelContainer>

Everything is working fine, except styling the individual items. In this case I am trying to add a margin to the radiobuttons. I am aware I could add the margins to each and every item manually, but I'm trying to avoid this.

I have tried putting the styles in ItemsPresenter.Resources of the main style and I have tried putting it in StackPanel.Resources of the ItemsPanelTemplate. Both of those options are not working, the styles are not applied.

Any ideas why this is not working?

1

There are 1 answers

0
Jordy On BEST ANSWER

I figured it out: I had to put the styles for the RadioButtons and CheckBoxes in the Style.Resources of the custom ItemsControl.

This is the working code:

<ItemsPanelTemplate x:Key="ItemsPanelTemplate">
    <StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>

<Style TargetType="local:LabelContainer">
    <Setter Property="ItemsPanel" Value="{StaticResource ItemsPanelTemplate}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:LabelContainer">
                <StackPanel>
                    <TextBlock FontWeight="Medium" Margin="0,0,7,6" Text="{TemplateBinding Label}"/>
                    <ItemsPresenter />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Resources>
        <Style TargetType="{x:Type RadioButton}" BasedOn="{StaticResource {x:Type RadioButton}}">
            <Setter Property="Margin" Value="0,0,8,0" />
        </Style>
        <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource {x:Type CheckBox}}">
            <Setter Property="Margin" Value="0,0,8,0" />
        </Style>
    </Style.Resources>
</Style>