我是WPF的新手,我正在使用一些DevExpress控件 我试图在某些按钮上实现样式,但总是显示无法解析资源的错误。
主窗口:
<dx:DXWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" x:Class="LicenceManagerWPF.MainWindow"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" dx:ThemeManager.ThemeName="Office2016"
Title="MainWindow" Height="746.218" Width="1139.154"
WindowStartupLocation="CenterScreen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80"/>
</Grid.RowDefinitions>
<StackPanel x:Name="Stack_Top" Orientation="Horizontal" Grid.Row="1" >
<dx:SimpleButton x:Name="btnRefresh" Style="{StaticResource ResourceKey=CustomStyles}" Width="55" ToolTip="Refresh" Margin="10,10,10,10" Glyph="{dx:DXImage Image=Refresh_32x32.png}" Content="Resfresh" />
<dx:SimpleButton x:Name="btndNew" Width="55" ToolTip="New Customer" Margin="10" Glyph="{dx:DXImage Image=NewContact_32x32.png}" Content="New Customer" />
<dx:SimpleButton x:Name="btnDelete" ToolTip="Delete Customer" Width="55" Margin="10" Content="Delete" Glyph="{dx:DXImage Image=Cancel_32x32.png}"/>
</StackPanel>
</Grid>
这是App.xaml
<Application x:Class="LicenceManagerWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
Startup="OnAppStartup_UpdateThemeName">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Name="CustomStyles" Source="StyleResource.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
这是我的样式文件
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LicenceManagerWPF"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" x:Class="LicenceManagerWPF.MainWindow"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core">
<Style x:Name="HeaderButtons" TargetType="dx:SimpleButton">
<Setter Property="Margin" Value="10"/>
<Setter Property="Width" Value="55"/>
</Style>
我正在搜索,一切看起来都很好,但我不知道为什么它无法解决。 此致
答案 0 :(得分:2)
无需为ResourceDictionary命名,只需提供其来源:
<ResourceDictionary Source="StyleResource.xaml"/>
ResourceDictionary项应该有x:Key
。如果是Style,如果没有显式键,则TargetType将用作键。这是一种创建默认样式的方法。
如果您想要命名样式,请设置x:Key
<Style x:Key="HeaderButtons" TargetType="dx:SimpleButton">
<Setter Property="Margin" Value="10"/>
<Setter Property="Width" Value="55"/>
</Style>
最后,StaticResource扩展按资源键引用资源,而不是名称:
Style="{StaticResource HeaderButtons}"
另外:为按钮设置样式时,边距和宽度设置(Width="55" Margin="10,10,10,10"
)变得多余。它们可以用来覆盖样式设置,但在这种情况下它们是相同的,为什么要写它们呢?