C# 如何在自定义ItemsControl的ItemsPresenter中设置项目的样式?

C# 如何在自定义ItemsControl的ItemsPresenter中设置项目的样式?,c#,wpf,xaml,itemscontrol,itemspresenter,C#,Wpf,Xaml,Itemscontrol,Itemspresenter,我正在尝试创建一个自定义ItemsControl,它显示单选按钮或复选框,并自动将样式应用于这些项目 这就是我现在拥有的: 自定义项控件: public class LabelContainer : ItemsControl { public string Label { get { return (String)GetValue(LabelProperty); } set { SetValue(LabelProperty, value); }

我正在尝试创建一个自定义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(""));
}
风格:

<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>

我是这样使用它的:

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

Nr 1
Nr 2
一切都很好,除了个别项目的样式。在本例中,我试图为单选按钮添加边距。我知道我可以手动为每一项添加边距,但我正试图避免这种情况

我已经尝试将样式放在主样式的ItemsPresenter.Resources中,并尝试将其放在ItemsPanelTemplate的StackPanel.Resources中。这两个选项都不起作用,因此未应用样式


你知道为什么这样不行吗?

我想出来了:我必须把单选按钮和复选框的样式放在自定义项控件的Style.Resources中

这是工作代码:

<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>