2011-04-13 159 views
1

我有一個創建資源字典作爲動態資源

<ResourceDictionary x:Class="RPK.WindowsResources" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:vm="clr-namespace:RPK.ViewModel" 
    xmlns:vw="clr-namespace:RPK.View" 
    xmlns:Converter="clr-namespace:RPK.Common"> 

    <sys:String x:Key="Key_Combo_Big_Width">200</sys:String> 

<Style x:Key="ComboBig"> 
     <Setter Property="Control.Width" Value="{DynamicResource ResourceKey=Key_Combo_Big_Width}"> 
     </Setter> 
     <Setter Property="Control.Height" Value="25"></Setter> 
     <Setter Property="Control.VerticalAlignment" Value="Center"></Setter> 
    </Style> 
</ResourceDictionary> 

我在我的App.xaml

在我window1.xaml應用這個作爲合併後的字典,我已經申請本風格

<ComboBox Name="Combo1" Style="{StaticResource ComboBig}"/> 

當我運行代碼,我得到這個錯誤

'200' is not a valid value for property 'Width'

什麼是正確的方法?

回答

2

Width屬性是一個double屬性,所以如果要爲其指定一個特定類型的資源值,則需要使用雙重資源。

<sys:Double x:Key="Key_Combo_Big_Width">200</sys:Double> 

    <Style x:Key="ComboBig"> 
     <Setter Property="Control.Width" Value="{DynamicResource Key_Combo_Big_Width}"> 
     </Setter> 
     <Setter Property="Control.Height" Value="25"></Setter> 
     <Setter Property="Control.VerticalAlignment" Value="Center"></Setter> 
    </Style>