添加样式到按钮

时间:2017-09-01 10:54:30

标签: wpf xaml

<ResourceDictionary
        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/interactivedesigner/2006"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">

        <Style TargetType="{x:Type TabControl}">
            <Setter Property="Margin" Value="0,5" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabControl}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Grid Grid.Row="0">
                                <Button Style="{StaticResource Button}"/>
                                <Button />
                            </Grid>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TabItem">
                        <Grid Name="xGrid">
                            <Border>
                                <ContentPresenter/>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="Button" TargetType="{x:Type Button}">
            <Setter Property="Background" Value="#FF000000" />
        </Style>
</ResourceDictionary>

我试图为按钮添加样式,但如果我在项目中添加它(<Button Grid.Column="0" Style="{StaticResource Button}"/>),我的项目就不会运行,并且VS不会显示我是一个例外。

我的样式保存在单独的文件中。

为什么我不能为按钮添加样式? VS没有告诉我什么例外?

我会接受任何帮助。

感谢。

P.S。对不起,如果有的话。

编辑#1:

的App.xaml:

<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"
             StartupUri="View\MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

编辑#2:

MainWindow.xaml:

<Window
    x:Class="WpfApp1.MainWindow"
    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:local="clr-namespace:WpfApp1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="525"
    Height="350"
    AllowsTransparency="True"
    Background="Transparent"
    WindowStyle="None"
    mc:Ignorable="d">

            <Grid>
                <TabControl>
                    <TabItem Header="jhb">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                        </Grid>
                    </TabItem>
                    <TabItem Header="TabItem" />
                    <TabItem Header="TabItem" />
                    <TabItem Header="TabItem" />
                    <TabItem Header="TabItem" />
                    <TabItem Header="TabItem" />
                    <TabItem Header="TabItem" />
                    <TabItem Header="TabItem" />
                    <TabItem Header="TabItem" />
                    <TabItem Header="TabItem" />
                    <TabItem Header="TabItem" />
                </TabControl>
            </Grid>
  </Window>

2 个答案:

答案 0 :(得分:0)

您是否已将ResourceDictionary合并到App.xaml

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApplication1"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Dictionary1.xaml替换为ResourceDictionary文件的实际名称。

答案 1 :(得分:0)