我將幾個圖像配置爲應用程序資源。在C#代碼中更改WPF窗口背景圖像
當我的應用程序啓動,主窗口的背景是通過XAML設置:
<Window.Background>
<ImageBrush ImageSource="/myapp;component/Images/icon.png" />
</Window.Background>
如果給定的事件發生時,我想這樣的背景更改爲另一個資源("/myapp;component/Images/icon_gray.png"
)。
我用兩個常量嘗試:
private static readonly ImageBrush ENABLED_BACKGROUND =
new ImageBrush(new BitmapImage(new Uri("/myapp;component/Images/icon.png")));
private static readonly ImageBrush DISABLED_BACKGROUND =
new ImageBrush(new BitmapImage(new Uri("/myapp;component/Images/icon_gray.png")));
...但當然,我得到無效的URI異常。
是否有一種簡單的方法可以使用pack Uri或資源(即:Myapp.Properties.Resources.icon
)更改WPF窗口的背景圖像(通過this.Background = ...
)?
第二個,因爲我定義了一個靜態常量。謝謝! – biasedbit 2010-10-24 19:36:33