WPF 中的全局样式
我正在学习如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据此问题的答案: 使用样式设置工具栏按钮大小
工具栏正在将自己的样式应用于按钮。可以通过资源键
ToolBar.ButtonStyleKey
访问此样式。使用该键设置您的样式,而不仅仅是定位Button
类型,您将被设置: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 theButton
type, and you'll be set:已经有一个涉及全局样式的答案,如果您想为每个按钮显式定义样式(也许在您的项目的未来),您需要命名该样式:
然后将其应用到您想要使用的按钮风格通过:
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:
and then apply it to a button you want to use the style via: