动态绑定到“路径”.资源的

本文关键字:资源 路径 动态绑定 | 更新日期: 2023-09-27 18:05:49

首先是我开始的代码:

<ribbon:RibbonMenuButton IsEnabled="{Binding ForegroundIsConfigurable}"
          SmallImageSource="{Binding Source={StaticResource imageSource},
                             Path=Source,
                             UpdateSourceTrigger=OnPropertyChanged}">

虽然这个绑定正在编译和运行良好,但我不满意的原因是imageSource在运行时发生了变化。

StaticResource标记扩展:通过查找对已定义资源的引用,为任何XAML属性提供值。该资源的查找行为类似于加载时查找,它将查找以前从当前XAML页面的标记以及其他应用程序源加载的资源,并将该资源值作为运行时对象中的属性值生成。

由于imageSource值在运行期间变化,我不得不将StaticResource更改为DynamicResource。但是属性Source不是一个依赖属性,因此下面的代码将引发一个运行时错误:

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

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

SmallImageSource="{DynamicResource imageSource}"

这将再次引发运行时错误,因为imageSourceImage的类型。SmallImageSource期望的值是ImageSource的类型

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

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

在考虑如何继续下去的时候,我突然想到,幸运的是,我可以把ImageSource的繁琐程序移到IValueConverter中。在我的RibbonMenuButton的给定数据上下文中,我有一个具有适当值的字符串值,这实际上也是我的ImageSource的来源。

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


imageSource与DrawingImage msdn页面上的XAML示例非常相似。

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

动态绑定到“路径”.资源的

您可以尝试将"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而不用重写IsEnabled,通过使用ElementName绑定,如下所示。

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