2017-08-28 56 views
0

林在WPF和即時通訊新使用一些DevExpress的控件 我試着去實現一個風格的一些按鈕,但始終顯示錯誤的資源不能得到解決。資源「X」無法解析WPF

主窗口:

<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> 

我正在尋找,一切看起來很好,但我不明白它爲什麼不能解決。 問候

回答

2

沒有必要來命名的ResourceDictionary,只需提供其資料來源:

<ResourceDictionary Source="StyleResource.xaml"/> 

ResourceDictionary的項目應該有x:Key。如果Style沒有明確的鍵值,TargetType將被用作鍵值。這是創建默認樣式的一種方法。通過資源鍵的名稱主要

<Style x:Key="HeaderButtons" TargetType="dx:SimpleButton"> 
    <Setter Property="Margin" Value="10"/> 
    <Setter Property="Width" Value="55"/> 
</Style> 

最後靜態資源擴展引用的資源,而不是:

如果你想指定樣式,則x設置

Style="{StaticResource HeaderButtons}" 

另外:當你設置的樣式爲一個按鈕,保證金和寬度設置(Width="55" Margin="10,10,10,10")變得多餘。它們可以用來覆蓋樣式設置,但在這種情況下它們是相同的,所以爲什麼要寫它們?

+0

感謝灰,你幫我瞭解我的錯誤。 我想創造一些樣式的下一個項目使用,你知道一個最適宜的。 我不是在設計真的很不錯,我只是想創建一個小prokect顯示WPF的functuonality agains Windows窗體。 此致敬禮 –