代码之家  ›  专栏  ›  技术社区  ›  Saeed Asgari

WPF样式无法在多个控件上工作

  •  0
  • Saeed Asgari  · 技术社区  · 7 年前

    我在控件上使用wpf样式,这样我就可以同时在多个控件上使用一种样式。它通常有效。例如,我制作了一个saveButtonStyle,并将其应用于应用程序上的每个save按钮。但它在菜单项上不起作用。 我为我的菜单项做了一个样式,在菜单项旁边有一个图标。 这是它的一个屏幕截图。 enter image description here

    看起来也是这样 enter image description here

    enter image description here

      <Style x:Key="menuItemAlert" TargetType="{x:Type MenuItem}">
                <Setter Property="Icon">
                    <Setter.Value>
                        <Image Source="Content/AlertIcon.png" Width="20" Height="20" />
                    </Setter.Value>
                </Setter>
            </Style>
    

    下面是我如何将其应用于我的控件

    <MenuItem x:Name="customerContextMenuSetAlarm" Header="SetAlarm"  Style="{StaticResource menuItemAlert}" Click="customerContextMenuSetAlarm_Click"/>
    

    1 回复  |  直到 5 年前
        1
  •  2
  •   ASh aminescm    7 年前

    风格 menuItemAlert 仅创建一个实例 Image 并且只能在一个地方显示。为了克服这一问题,请为此创建一个单独的非共享资源 .

    <Image x:Key="AlertIcon" x:Shared="False" Source="Content/AlertIcon.png" Width="20" Height="20" />
    
    <Style x:Key="menuItemAlert" TargetType="{x:Type MenuItem}">
        <Setter Property="Icon" Value="{StaticResource AlertIcon}"/>
    </Style>