首先我从开始的代码的“路径”:动态绑定到一个资源
<ribbon:RibbonMenuButton IsEnabled="{Binding ForegroundIsConfigurable}"
SmallImageSource="{Binding Source={StaticResource imageSource},
Path=Source,
UpdateSourceTrigger=OnPropertyChanged}">
虽然这种结合正在编制和精细的运行我不满意的原因是运行时期间imageSource
变化。
StaticResource Markup Extension : Provides a value for any XAML property attribute by looking up a reference to an already defined resource. Lookup behavior for that resource is analogous to load-time lookup, which will look for resources that were previously loaded from the markup of the current XAML page as well as other application sources, and will generate that resource value as the property value in the run-time objects.
由于在运行期间imageSource
值改变,我不得不改变StaticResource
到DynamicResource
。但物业Source
不是一个依赖属性,因此下面的代码将引发一个运行时错误:
SmallImageSource="{Binding Source={DynamicResource imageSource},
Path=Source,
UpdateSourceTrigger=LostFocus}
出于这个原因,我需要动态资源直接绑定到SmallImageSource
,这是一个依赖属性:
SmallImageSource="{DynamicResource imageSource}"
这又会引起运行时错误,因为imageSource
是Image
的类型。 SmallImageSource
预计值为ImageSource
。
现在可以建议将数据上下文设置为我的动态资源并适当地绑定属性。如果我这样做,我会杀死另一个DataContext
的财产IsEnabled
的约束力。
据我所知,MultiBinding
也不是一个解决方案,因为它提供了一种机制来绑定一个属性对几个来源,但不提供绑定不同的属性对不同的上下文和来源。
在想着如何继续前进的时候,我想到幸运的是我可以将ImageSource
rigmarole移动到IValueConverter
。在我的RibbonMenuButton
给定的数据上下文中,我有一个字符串值与适当的值,这实际上也是我的ImageSource
的来源。
无论如何,我仍然想知道如果我没有其他方法,即如果两个数据源都处于不同的数据上下文中,我将如何解决这个问题。有什么我没有看到的?我如何确保不会通过覆盖DataContext
并通过绑定动态资源的属性来终止其他绑定?
的imageSource
是相当相同的DrawingImage msdn page的XAML例子。
<Image x:Key="imageSource">
<Image.Source>
<DrawingImage>
...
您可以将代码发布到您定义“imageSource”资源的位置吗? – 2013-03-26 18:11:45