gpt4 book ai didi

wpf - 为什么数据绑定(bind)到 DynamicResource 不起作用?

转载 作者:行者123 更新时间:2023-12-04 17:13:18 25 4
gpt4 key购买 nike

以下代码不起作用。我如何使它工作?

<Image Source="{DynamicResource {Binding VM.ImageKey}}" />

最佳答案

这是 DynamicResource MarkupExtension 的错误用法.正确的是:

<Image Source="{DynamicResource VM.ImageKey}" />

假设您有一个资源,其 key “VM.ImageKey”定义如下:
<Bla.Resources>
<BitmapImage x:Key="VM.ImageKey" UriSource="C:\Uri\To\Image.jpg" />
</Bla.Resources>

但是,如果您想绑定(bind)当前 DataContext 中的某些属性你不能使用 DynamicResource但是 Binding :
<Image Source="{Binding VM.ImageKey}" />

假设您当前的 DataContext是一个实例,它有一个名为 VM 的属性,它又具有一个名为 ImageKey 的属性,它是 ImageSource 的派生类型.

关于wpf - 为什么数据绑定(bind)到 DynamicResource 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3733505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com