WPF 中的全局样式

发布于 2024-12-05 04:41:44 字数 2346 浏览 1 评论 0原文

我正在学习如何在 WPF 中使用样式,根据我在网上找到的信息,如果我以这种方式定义样式(使用目标类型):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

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

...此样式将应用于我的应用程序中的所有按钮。然而我的按钮似乎保持不变。 在主窗口中,我将资源字典添加到资源集合中,并插入了几个按钮,但是我的按钮看起来始终相同。这是主窗口的代码:

<Window x:Class="MCTSTrainingChapter1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="GridResources.xaml"/>
                <ResourceDictionary Source="ButtonResources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <DockPanel >
        <ToolBar DockPanel.Dock="Top" Height="26" Name="toolBar1"  >
            <Button x:Name="btnBold"  Click="Button_Click">Bold</Button>
            <Button Click="Button_Click_1">Italic</Button>
            <Slider Name="slider1" Minimum="2" Maximum="72" Width="100" ValueChanged="slider1_ValueChanged"></Slider>
        </ToolBar>
        <Grid Name="grid1" Background="{StaticResource GridBackgroundBrush}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="5"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <ListBox Grid.Column="0" Name="listBox1" SelectionChanged="listBox1_SelectionChanged" />
            <GridSplitter Grid.Column="1" Margin="0" Width="5" HorizontalAlignment="Left"/>
            <RichTextBox Grid.Column="2" Name="richTextBox1"/>
        </Grid>

    </DockPanel>
</Window>

为什么没有应用按钮样式?

I'm learning how to use styles in WPF and according to information I found on the net, if I define style this way (with target type):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

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

...this style will be applied for all buttons in my application. However it seems that my buttons remain the same.
In main window I added resource dictionary to resources collection and I inserted a couple of buttons, however my buttons look the same all the time. Here is the code for main window:

<Window x:Class="MCTSTrainingChapter1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="GridResources.xaml"/>
                <ResourceDictionary Source="ButtonResources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <DockPanel >
        <ToolBar DockPanel.Dock="Top" Height="26" Name="toolBar1"  >
            <Button x:Name="btnBold"  Click="Button_Click">Bold</Button>
            <Button Click="Button_Click_1">Italic</Button>
            <Slider Name="slider1" Minimum="2" Maximum="72" Width="100" ValueChanged="slider1_ValueChanged"></Slider>
        </ToolBar>
        <Grid Name="grid1" Background="{StaticResource GridBackgroundBrush}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="5"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <ListBox Grid.Column="0" Name="listBox1" SelectionChanged="listBox1_SelectionChanged" />
            <GridSplitter Grid.Column="1" Margin="0" Width="5" HorizontalAlignment="Left"/>
            <RichTextBox Grid.Column="2" Name="richTextBox1"/>
        </Grid>

    </DockPanel>
</Window>

Why isn't the Button style applied?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲世九天 2024-12-12 04:41:45

根据此问题的答案: 使用样式设置工具栏按钮大小

工具栏正在将自己的样式应用于按钮。可以通过资源键 ToolBar.ButtonStyleKey 访问此样式。使用该键设置您的样式,而不仅仅是定位 Button 类型,您将被设置:

<Style x:Key="{x:Static ToolBar.ButtonStyleKey}" TargetType="Button">
    <Setter Property="Width" Value="100" />
</Style>

As per the answer to this question: Setting toolbar button sizes using a Style

The ToolBar is applying its own style to the button. This style can be accessed by the resource key ToolBar.ButtonStyleKey. Set up your Style with that key, rather than just Targeting the Button type, and you'll be set:

<Style x:Key="{x:Static ToolBar.ButtonStyleKey}" TargetType="Button">
    <Setter Property="Width" Value="100" />
</Style>
风吹过旳痕迹 2024-12-12 04:41:45

已经有一个涉及全局样式的答案,如果您想为每个按钮显式定义样式(也许在您的项目的未来),您需要命名该样式:

<Style x:Key="buttonStyleOne" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Black"/>
</Style>

然后将其应用到您想要使用的按钮风格通过:

<Button Style="{DynamicResource buttonStyleOne}">Content!</Button>

There is already an answer involving a global style, if you want to explicitly define your style for each button (perhaps in the future of your project) you would want to name the style:

<Style x:Key="buttonStyleOne" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Black"/>
</Style>

and then apply it to a button you want to use the style via:

<Button Style="{DynamicResource buttonStyleOne}">Content!</Button>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文