绑定到资源键,WPF [英] Binding to resource key, WPF

查看:22
本文介绍了绑定到资源键,WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些图像的 ResourceDictionary:

I have a ResourceDictionary with some images:

<BitmapImage UriSource="..ImagesBrightfolder-bright.png"
             x:Key="FolderItemImage" />

我为树视图项创建了一个 HierarchicalTemplate,如下所示:

I've create a HierarchicalTemplate for treeview items like the following:

<HierarchicalDataTemplate ItemsSource="{Binding VisibleChildren}"
                          DataType="{x:Type st:StructureTreeItem}">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding ImageResourceKey}" />
        <Image x:Name="iIcon2" Source="{DynamicResource FolderItemImage}"/>
        <Image x:Name="iIcon"
               Source="{DynamicResource {Binding ImageResourceKey}}"/>
    </StackPanel>
</HierarchicalDataTemplate>

现在,当显示项目时:

  • Textblock 显示 FolderItemImage
  • 显示的是第一张图片
  • 第二张图片未显示.

整个想法是将项目图像设置为存储在资源中的图像,但不幸的是,上面介绍的技术不起作用,现在我知道为什么了:

The whole idea is to set item images to ones stored in resources, but the technique presented above unfortunately won't work and now I know, why:

<Image x:Name="iIcon3" Width="16" Height="16" Margin="0, 1, 3, 1" >
    <Image.Source>
        <DynamicResource ResourceKey="{Binding ImageResourceKey}" />
    </Image.Source>
</Image>

结果:

PresentationFramework.dll 中发生类型为System.Windows.Markup.XamlParseException"的未处理异常

An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

附加信息:无法在DynamicResourceExtension"类型的ResourceKey"属性上设置Binding".只能在 DependencyObject 的 DependencyProperty 上设置绑定".

Additional information: A 'Binding' cannot be set on the 'ResourceKey' property of type 'DynamicResourceExtension'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

所以我必须重新表述我的问题:如何将模型中存储的一些数据(可能是资源键?)转换为动态资源?它必须是一个动态资源,因为我确信它可能会在运行时发生变化.

So I have to rephrase my question: how can I convert some data (a resource key, perhaps?) stored in model to a dynamic resource? It has to be a dynamic resource, because I'm certain it may change during runtime.

推荐答案

不能直接完成.还有另一种方法虽然涉及附加属性:

It cannot be done directly. There's another way though involving an attached property:

public static class ImageHelper {

    private static void SourceResourceKeyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {

        var element = d as Image;
        if (element != null) {

            element.SetResourceReference(Image.SourceProperty, e.NewValue);
        }
    }

    public static readonly DependencyProperty SourceResourceKeyProperty = DependencyProperty.RegisterAttached("SourceResourceKey",
        typeof(object),
        typeof(ImageHelper),
        new PropertyMetadata(String.Empty, SourceResourceKeyChanged));

    public static void SetSourceResourceKey(Image element, object value) {

        element.SetValue(SourceResourceKeyProperty, value);
    }

    public static object GetSourceResourceKey(Image element) {

        return element.GetValue(SourceResourceKeyProperty);
    }
}

然后:

<Image local:ImageHelper.SourceResourceKey="{Binding SomeValue}" />

这篇关于绑定到资源键,WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆