如何在XAML中调用ResourceDictionary文件中的样式?

如何在XAML中调用ResourceDictionary文件中的样式?,xaml,resourcedictionary,wpf-style,Xaml,Resourcedictionary,Wpf Style,我在创建的style.xaml ResourceDictionary文件中有一个按钮样式 我用这个代码来称呼它: <Button Style="{DynamicResource exitButton}" /> 但是它也没有识别出样式键,使用StaticResource也不起作用。如何解决这个问题 我的样式代码: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentatio

我在创建的style.xaml ResourceDictionary文件中有一个按钮样式

我用这个代码来称呼它:

<Button Style="{DynamicResource exitButton}" />
但是它也没有识别出样式键,使用StaticResource也不起作用。如何解决这个问题

我的样式代码:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Style x:Key="exitButton" TargetType="Button">
    <Setter Property="Width" Value="22"/>
    <Setter Property="Height" Value="32"/>
    <Setter Property="Background" Value="#FF7070"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="Button">
          <Border Width="{TemplateBinding Width}"
                  Height="{TemplateBinding Height}"
                  HorizontalAlignment="Center"
                  VerticalAlignment="Center">
            <TextBlock Text="X"
                       FontSize="15"
                       Foreground="White"
                       FontWeight="Bold"/>
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>
您必须将ResourceDictionary文件导入xaml中的Resources标记中

大概是这样的:

<UserControl blablabla...>
  <UserControl.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/*PROJECT_NAME*;component/*FOLDER_PATH*/style.xaml"/>
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </USerControl.Resources>

  <!-- the content -->

  ...

  <Button Style="{StaticResource exitButton}"/>

</UserControl>
您必须将ResourceDictionary文件导入xaml中的Resources标记中

大概是这样的:

<UserControl blablabla...>
  <UserControl.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/*PROJECT_NAME*;component/*FOLDER_PATH*/style.xaml"/>
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </USerControl.Resources>

  <!-- the content -->

  ...

  <Button Style="{StaticResource exitButton}"/>

</UserControl>
您有两个选择:

正如HasNotifications所说,将资源嵌入到您希望样式生效的视图中 将样式嵌入到应用程序ResourceDictionary中。在这种情况下,样式将可用于应用程序上的任何视图 将以下代码添加到App.xaml文件:

您有两个选择:

正如HasNotifications所说,将资源嵌入到您希望样式生效的视图中 将样式嵌入到应用程序ResourceDictionary中。在这种情况下,样式将可用于应用程序上的任何视图 将以下代码添加到App.xaml文件:

可能的重复可能的重复