我正在尝试使用以下xaml将RibbonGroup和几个RibbonButton绑定到我的视图模型:

<Style TargetType="{x:Type ribbon:RibbonGroup}" x:Key="RibbonGroupStyle">
    <Setter Property="Header" Value="{Binding Header}" />
    <Setter Property="ItemContainerStyle" Value="{DynamicResource RibbonButtonStyle}" />
    <Setter Property="ItemsSource" Value="{Binding Buttons}" />
</Style>

<Style TargetType="{x:Type ribbon:RibbonButton}" x:Key="RibbonButtonStyle">
    <Setter Property="Label" Value="{Binding Header}" />
</Style>


这给了我我可以理解的以下错误,但是如何将RibbonButton的Label正确绑定到我的视图模型?

A style intended for type 'RibbonButton' cannot be applied to type 'RibbonControl'.

最佳答案

您可以将一种样式放入另一种样式中,并将其应用于每个按钮:

<Style TargetType="{x:Type ribbon:RibbonGroup}" x:Key="RibbonGroupStyle">
    <Style.Resources>
        <Style TargetType="{x:Type ribbon:RibbonButton}" BasedOn="{StaticResource {x:Type ribbon:RibbonButton}">
            <Setter Property="Label" Value="{Binding Header}" />
        </Style>
    </Style.Resources>

    <Setter Property="Header" Value="{Binding Header}" />
    <Setter Property="ItemsSource" Value="{Binding Buttons}" />
</Style>

关于c# - RibbonGroup的ItemContainerStyle,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15878871/

10-10 21:31