2013-03-26 79 views
6

首先我从开始的代码的“路径”:动态绑定到一个资源

<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值改变,我不得不改变StaticResourceDynamicResource。但物业Source不是一个依赖属性,因此下面的代码将引发一个运行时错误:

SmallImageSource="{Binding Source={DynamicResource imageSource}, 
        Path=Source, 
        UpdateSourceTrigger=LostFocus} 

出于这个原因,我需要动态资源直接绑定到SmallImageSource,这是一个依赖属性:

SmallImageSource="{DynamicResource imageSource}" 

这又会引起运行时错误,因为imageSourceImage的类型。 SmallImageSource预计值为ImageSource

现在可以建议将数据上下文设置为我的动态资源并适当地绑定属性。如果我这样做,我会杀死另一个DataContext的财产IsEnabled的约束力。

据我所知,MultiBinding也不是一个解决方案,因为它提供了一种机制来绑定一个属性对几个来源,但不提供绑定不同的属性对不同的上下文和来源。

在想着如何继续前进的时候,我想到幸运的是我可以将ImageSource rigmarole移动到IValueConverter。在我的RibbonMenuButton给定的数据上下文中,我有一个字符串值与适当的值,这实际上也是我的ImageSource的来源。

无论如何,我仍然想知道如果我没有其他方法,即如果两个数据源都处于不同的数据上下文中,我将如何解决这个问题。有什么我没有看到的?我如何确保不会通过覆盖DataContext并通过绑定动态资源的属性来终止其他绑定?


imageSource是相当相同的DrawingImage msdn page的XAML例子。

<Image x:Key="imageSource"> 
    <Image.Source> 
    <DrawingImage> 
... 
+0

您可以将代码发布到您定义“imageSource”资源的位置吗? – 2013-03-26 18:11:45

回答

1

你可以尝试界定 “imageResource” 作为ImageSource而不是一个Image。这对我有用。

<r:RibbonWindow 
    x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary" 
    xmlns:pc="clr-namespace:System.Windows.Media;assembly=PresentationCore"> 
    <Grid> 
     <Grid.Resources> 
      <pc:ImageSource x:Key="imageSource">your_image.png</pc:ImageSource> 
     </Grid.Resources> 
     <r:Ribbon> 
      <r:RibbonMenuButton 
       IsEnabled="{Binding ForegroundIsConfigurable}" 
       SmallImageSource="{DynamicResource imageSource}"> 
      </r:RibbonMenuButton> 
     </r:Ribbon> 
    </Grid> 
</r:RibbonWindow> 

此外,您可以设置您的RibbonMenuButton的DataContext的,而不通过使用绑定的ElementName如下覆盖的IsEnabled。

<r:RibbonWindow x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary" 
     Title="MainWindow" Height="350" Width="525"> 

    <Grid x:Name="root"> 

     <Grid.Resources> 
      <Image x:Key="imageSource" Source="{Binding myImageSource}"/> 
     </Grid.Resources> 

     <r:Ribbon> 
      <r:RibbonMenuButton DataContext="{DynamicResource imageSource}" 
       IsEnabled="{Binding ElementName=Root, Path=DataContext.ForegroundIsConfigurable}" 
       SmallImageSource="{Binding Source}"/> 
     </r:Ribbon> 
    </Grid> 
</r:RibbonWindow> 
+0

我认为第二个想法可行。明天会试试。然而,第一个想法是行不通的。我忘了提及我自己画的图像,没有图像链接到。 – Em1 2013-03-26 21:49:53

+0

@ Em1关于第一个想法,如果您可以从描绘图像的代码访问资源字典,则可以即时更新资源字典的imageSource密钥。 – 2013-03-26 22:05:01

+0

我刚刚测试了第二种方法。出于某种原因,它不适用于ElementName,但使用RelativeSource。 - 我在XAML中绘制图像,我更新了有关这个问题。我没有调试到直接使用ImageSource(而不是Image.Source)时无法添加内容的问题。 – Em1 2013-03-27 09:02:53