【问题标题】:WPF use binding to assign static resourceWPF 使用绑定分配静态资源
【发布时间】:2013-11-29 06:00:41
【问题描述】:

我正在尝试使用枚举来显示相应的图像。为此,我有一个值转换器,可将枚举转换为正确的资源名称。我的资源定义如下:

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

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    我会在转换器中返回资源:

    <Image Source="{Binding CurrentAlarmItem.AlarmCategory, Converter={StaticResource AlarmCategoryConverter}}" />
    

    在您的转换器中执行以下操作:

    return Application.Current.FindResource("AlarmCat1") as BitmapImage;
    

    使用 resourcedictionary (app.xaml) 为整个应用程序设置资源

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    

    在您的字典中 (Dictionary1.xaml)

    <BitmapImage x:Key="AlarmCat1" UriSource="bh.jpg" />
    

    因为您的资源现在是在应用程序级别定义的,所以代码现在将找到您的资源并将其返回。

    【讨论】:

    • 刚刚删除了我的转换器...但我也将尝试您的解决方案。看起来更直观
    • 它可能不起作用。我相信你可以让它与其他一些代码一起工作,但如果上面的答案有效,我应该使用它;-)
    • 好吧,你的解决方案是一行xaml,上面是17行....我可以移动资源,但是我应该使用User.Control中指定的名称BitmapImage在我原来的问题中看到?还是文件名?
    • 使它与我的答案一起使用的最简单方法是更改​​资源的位置。 (我更新了我的答案)
    • 无需本地化资源。我可以按照您的描述将资源定义从我的用户控件添加到资源字典中,并且它可以工作。美在于它在设计时也有效!
    【解决方案2】:

    您不能绑定StaticResource 密钥,因为它不是DependancyProperty。要么您必须使用 converterSource 直接绑定到枚举并更新转换器代码以返回 Bitmap 本身。

    第二个选项是使用Triggers 根据enum 值设置Source 属性。

    <Image >
       <Image.Style>
          <Style TargetType="{x:Type Image}">
             <Style.Triggers>
                <DataTrigger Binding="{Binding CurrentAlarmItem.AlarmCategory}" 
                             Value="Category1">
                   <Setter Property="Source" Value="{StaticResource AlarmCat1}" />
                </DataTrigger>
             </Style.Triggers>
          </Style>
       </Image.Style>
    </Image>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-12
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      • 2023-02-22
      相关资源
      最近更新 更多