我想使用枚舉來顯示相應的圖像。爲此,我有一個值轉換器將枚舉轉換爲正確的資源名稱。我的資源定義如下:WPF使用綁定來分配靜態資源
<UserControl.Resources>
<BitmapImage x:Key="AlarmCat1" UriSource="/Lib.Infrastructure;component/Resources/msg_cat1.bmp" />
<BitmapImage x:Key="AlarmCat2" UriSource="/Lib.Infrastructure;component/Resources/msg_cat2.bmp" />
<BitmapImage x:Key="AlarmCat3" UriSource="/Lib.Infrastructure;component/Resources/msg_cat3.bmp" />
<converters:JamCategoryToImageConverter x:Key="AlarmCategoryConverter" />
</UserControl.Resources>
這工作:
<Image Source="{StaticResource AlarmCat1}" />
但這不,轉換器被稱爲和正確的值傳遞迴。什麼是正確的語法?
<Image Source="{StaticResource { Binding CurrentAlarmItem.AlarmCategory, Converter={StaticResource AlarmCategoryConverter}}}" />
爲了完整,這是轉換功能:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch ((AlarmCategory)value)
{
case AlarmCategory.Category1:
return "AlarmCat1";
case AlarmCategory.Category2:
return "AlarmCat2";
case AlarmCategory.Category3:
return "AlarmCat3";
default:
return null;
}
}
剛剛刪除了我的轉換器...但我也要嘗試您的解決方案。看起來更直觀 – uncletall
由於資源不在同一個程序集中,因此拋出異常。 – uncletall
它可能不起作用。我相信你可以得到它與其他代碼的一些工作,但如果上述答案的工作,我應該使用;-) –