0

我希望将样式应用于所有 WPF 控件,例如在 HTML 中使用 CSS 可以完成的操作。

这是我第一次涉足 WPF,我收集到我需要在 App.xaml 中执行此操作。下面是我所拥有的。

我试过TargetType="{x:Type TabItem}"TargetType="TabItem"

我定义的样式都没有被应用。

应用程序.xaml

<Application x:Class="VMware_Lab_Manager_Desktop.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary Source="Styles.xaml"></ResourceDictionary>
    </Application.Resources>
</Application>

样式.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type TabControl}">
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF464646" Offset="0" />
                    <GradientStop Color="#FF2D2D2D" Offset="0.5" />
                    <GradientStop Color="#FF141414" Offset="1" />
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type TabItem}">
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF464646" Offset="0" />
                    <GradientStop Color="#FF2D2D2D" Offset="0.5" />
                    <GradientStop Color="#FF141414" Offset="1" />
                </LinearGradientBrush>

            </Setter.Value>
        </Setter>
        <Setter Property="BorderBrush">
            <Setter.Value>
                <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                    <GradientStop Color="Black" Offset="0" />
                    <GradientStop Color="#FF464646" Offset="1" />
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type Window}">
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                    <GradientStop Color="#FF2D2D2D" Offset="0.5" />
                    <GradientStop Color="#FF141414" Offset="0" />
                    <GradientStop Color="#FF464646" Offset="1" />
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

主窗口.xaml

<Window x:Class="VMware_Lab_Manager_Desktop.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="456" Width="803" Padding="0" Margin="0">

    <TabControl HorizontalAlignment="Stretch" Margin="0" Padding="0" Name="tabControl1" VerticalAlignment="Stretch" BorderThickness="0" TabStripPlacement="Bottom">
        <TabItem Name="tabItem1" Header="...">
            <WebBrowser  HorizontalAlignment="Stretch" Name="webBrowser1" Margin="0" VerticalAlignment="Stretch" Source="http://www.msdn.com/" />
        </TabItem>
        <TabItem Header="+" />
    </TabControl>
</Window>
4

1 回答 1

2
 <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Style.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
于 2013-05-19T00:23:58.257 回答