在 DataTemplate 中重用样式

问题描述

我有一个 UserControl,它重用了在另一个 xaml 中定义的样式。该样式使用可以在我的“标准”viewmodel 中找到的值。我想对我的 UserControl 上的 ItemsControl 中的元素(按钮)应用这种样式。换句话说,我想重用模板中的样式。

我知道数据模板的数据上下文与 UserControl 使用的不同。如何为要操作的样式指定此数据上下文?

我不想在这里重复原始 style.xaml 中的任何内容。具体见代码注释。

<UserControl x:Class="LF.NSCGPModule.CCPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:nscgpModule="clr-namespace:LF.NSCGPModule"
xmlns:nsgpModule="clr-namespace:LF.NSGPModule;assembly=GP"
MinHeight="110" MinWidth="200"
FontFamily="{Binding FontFamily}" FontSize="{Binding FontSize}"
d:DataContext="{d:DesignInstance nscgpModule:MainWindowviewmodel}"
>

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ROFStyles.xaml" /> <!-- Style reused here. Setters use Bindings from 'MainWindowviewmodel'-->
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

</UserControl.Resources>

<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
    <StackPanel Orientation="Vertical">

        <Button>TEST1</Button>
        <Button Style="{StaticResource buttonStyleCommand}">TEST2</Button> <!-- style OK here -->
        <Button Height="{Binding Path=CCPanelviewmodel.CommandButtonHeight}">TEST3</Button> <!-- relative binding works here-->

        <ItemsControl ItemsSource="{Binding CCPanelviewmodel.Row1Descriptors}">
            
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="nsgpModule:GPCommandDescriptor">
                    <DataTemplate.Resources>
                        <Style targettype="Button" BasedOn="{StaticResource buttonStyleCommand}">
                            <!-- Style actually found but the setters dont find the bound values - they should search in the MainWindowviewmodel
                            and not in the datacontext(?) of the template-->
                        </Style>
                    </DataTemplate.Resources>

                    <!-- This is the UIElement I want to style. Relative binding for the Height property works OK -->
                    <Button CommandParameter="{Binding Path=PlayerActionCode}" Content="{Binding Path=Content}"
                            IsEnabled="{Binding Path=IsEnabled}" Click="CommandButtonClick"
                            Height="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl},Path=DataContext.CCPanelviewmodel.CommandButtonHeight}"
                            ToolTip="{Binding Path=ToolTip}"
                    />

                </DataTemplate>
            </ItemsControl.ItemTemplate>

            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </StackPanel>
</ScrollViewer>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)