Dynamic linking to the "Path" of the resource

First enter the code I started from:

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

While this binding compiles and works fine, I am not satisfied that imageSource changes at runtime.

StaticResource extension extension : provides a value for any attribute of the XAML property by looking at a link to an already defined resource. The search behavior for this resource is similar to search by load time, which will search for resources that were previously loaded from the markup of the current XAML page, as well as other application sources and generates this resource value as a property value in the -time run of objects.

Since changing the value of imageSource at runtime, I had to change StaticResource to DynamicResource . But the Source property is not a dependency property, and therefore, the following code will throw a runtime error:

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

For this reason, I need to associate a dynamic resource directly with SmallImageSource , which is a dependency property:

 SmallImageSource="{DynamicResource imageSource}" 

This will again cause a runtime error, since imageSource is an Image type. SmallImageSource expects the value to be of type imageSource .

Now you can offer to set the data context for my dynamic resource and bind it accordingly. If I do this, I will kill the binding of the IsEnabled property, which has another DataContext .

And as far as I know, MultiBinding also not a solution, because it provides a mechanism for binding properties to several sources, but does not provide binding of different properties to different contexts and sources.

While I was thinking about how to do this, it occurred to me that, fortunately, I can move imageSource rigmarole to an IValueConverter . In this data context of my RibbonMenuButton , I have a string value with the corresponding value, which is actually also the source of my imageSource .

In any case, I’m still wondering how I would solve the problem if I didn’t have a different approach, i.e. if both sources were in different data contexts. Is there something I don't see? How can I guarantee not to kill another binding by overwriting the DataContext and though binding to the property of the dynamic resource?


imageSource is similar to the XAML example on the DrawingImage msdn page.

 <Image x:Key="imageSource"> <Image.Source> <DrawingImage> ... 
+6
source share
1 answer

You can try to define "imageResource" as ImageSource instead of Image . This works for me.

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

Alternatively, you can set the DataContext of your RibbonMenuButton without overriding IsEnabled using the ElementName binding, as shown below.

 <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> 
+1
source

All Articles