wpf 如何在ItemsControl的控件模板中设置堆栈面板的样式?

kognpnkq  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(203)

我试图定义一个自定义控件,它承载StackPanel中的所有子元素(在我的例子中是子元素),其中StackPanel应该应用并扩展按钮样式

<ItemsControl.Resources>
    <Style TargetType="sharedUserControls:ButtonBarControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="sharedUserControls:ButtonBarControl">
                    <StackPanel 
                        Orientation="Horizontal" 
                        IsItemsHost="True"
                        FlowDirection="{Binding Path=ButtonBarFlowDirection, ElementName=ButtonBar}">
                        <StackPanel.Resources>
                            <Style TargetType="Button" BasedOn="{StaticResource DialogButton}">
                                <Setter Property="Margin" Value="{Binding Path=ButtonMargin, ElementName=ButtonBar}"/>
                            </Style>
                        </StackPanel.Resources>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ItemsControl.Resources>

字符串
第一部分工作,我可以使用这个自定义控件的方式,我想,但风格的按钮是不适用于堆栈面板。为什么不呢?
我也试

<ItemsControl.Resources>
    <Style TargetType="sharedUserControls:ButtonBarControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="sharedUserControls:ButtonBarControl">
                    <StackPanel 
                        Style="{StaticResource ButtonBarStackPanel}"
                        Orientation="Horizontal" 
                        IsItemsHost="True"
                        FlowDirection="{Binding Path=ButtonBarFlowDirection, ElementName=ButtonBar}">
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ItemsControl.Resources>


在我的Styles.xaml中,

<Style TargetType="StackPanel" x:Key="ButtonBarStackPanel">
    <Style.Resources>
        <Style TargetType="Button" BasedOn="{StaticResource DialogButton}">
            <Setter Property="Margin" Value="5"/>
        </Style>
    </Style.Resources>
</Style>


但这给出了相同的结果,我找不到任何方法来应用所需的样式对堆栈面板的儿童(按钮)

0mkxixxg

0mkxixxg1#

我提出了以下解决方案,我只需指定ControlTemplate.Resources

<ItemsControl x:Class="Common.Utilities.Git.UI.SharedUserControls.ButtonBarControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:sharedUserControls="clr-namespace:Common.Utilities.Git.UI.SharedUserControls"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300"
             x:Name="ButtonBar">
    <ItemsControl.Resources>
        <Style TargetType="sharedUserControls:ButtonBarControl">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="sharedUserControls:ButtonBarControl">
                        <ControlTemplate.Resources>
                            <Style TargetType="Button" BasedOn="{StaticResource DialogButton}">
                                <Setter Property="Margin" Value="{Binding Path=ButtonMargin, ElementName=ButtonBar}"/>
                            </Style>
                        </ControlTemplate.Resources>
                        <StackPanel 
                            Orientation="Horizontal" 
                            IsItemsHost="True"
                            FlowDirection="{Binding Path=ButtonBarFlowDirection, ElementName=ButtonBar}">
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ItemsControl.Resources>
</ItemsControl>

字符串
这让我可以这样使用它

<sharedUserControls:ButtonBarControl
        ButtonMargin="5"
        ButtonBarFlowDirection="RightToLeft">
                
        <Button Command="{Binding CloseCommand}" Content="{lex:Loc Close}" />
        <Button Command="{Binding UpdateCommand}" Content="{lex:Loc OpenDocumentManagementUpdateSelectedDocumentDefinition}" />
        <Button Command="{Binding CreateNewDocumentDefinitionCommand}" Content="{lex:Loc NewDocumentDefinitionButtonText}" />
    </sharedUserControls:ButtonBarControl>

相关问题