2014-09-26 68 views
0

我想我的顏色與窗口systemColors中,但是當我這樣做是這樣的:C#WPF systemColors中不能正常工作

<Menu 
    Width="Auto" 
    Height="20" 
    Background="{DynamicResource {x:Static SystemColors.MenuBarColor}}"/> 

我得到一個8位hexnumber。我的程序只接受3或6個顏色的數字。執行程序總是給人一個錯誤信息

System.Windows.Markup.XamlParseException: Zeilennummer "11" und Zeilenposition "4" 
von "Beim Festlegen der Eigenschaft" System.Windows.Controls.Panel.Background 
"wurde eine Ausnahme ausgelöst.". ---> System.ArgumentException: "#FFF0F0F0" ist 
kein gültiger Wert für die Eigenschaft "Background". 

,如果我使用「MenuBarColorKey」,而不是這也不會被接受爲vaild值。但隨後文本顯示「」MenuBarColorKey「不是有效值......」

我該如何糾正這個問題?

感謝您的幫助

邁克爾

編輯: @AwkwardCoder

對不起,已經讀了幾年,我是新來的計算器寫作,所以我無法評論或投票。

你說得對。用刷子工作。非常感謝你!

但我提到的名字從 扎克·彼得森的效用如圖

「WPF - ?我如何使用正確的Windows系統顏色」

那裏,我沒有找到擴展名「刷」。這個工具適用於我。

+1

你試過'SystemColors.MenuBarBrushKey'呢? – 2014-09-26 09:42:22

+0

在WPF中,顏色的8位十六進制數字與程序使用的6位數字相同,但是與顏色的Alpha通道(不透明度)相關的2位額外數字(在開始處):'#AARRGGBB'。 – Sheridan 2014-09-26 10:33:24

回答

2

這裏有2個問題。首先,您希望使用Color作爲Brush屬性,然後嘗試使用Color作爲資源關鍵字。

如果你想使用它,你需要使用SystemColors.MenuBarBrushKey

Background="{DynamicResource {x:Static SystemColors.MenuBarBrushKey}}" 

的資源,或者你可以直接使用它,那麼你應該使用SystemColors.MenuBarBrush

Background="{x:Static SystemColors.MenuBarBrush}" 

你會發現,SystemColors爲您提供每種顏色的4個靜態屬性

  • XxxxColor給你Color結構
  • XxxxColorKey給你ResourceKeyColor結構
  • XxxxBrush給你SolidColorBrush
  • XxxxBrushKey給你ResourceKeySolidColorBrush