Bind the source image property from the view model to a key in the resource dictionary
Posted: 30 Oct 2023, 21:12
Hello!
I want to bind the source image property from the view model to a key in the resource dictionary in the Atlas.
Something like this:
This is the structure of the Atlas:
I tried using this converter, but I can't find the resource:
I want to know how to bind static resource keys in the Atlas from the view model.
How should I handle this approach?
I want to bind the source image property from the view model to a key in the resource dictionary in the Atlas.
Something like this:
Code: Select all
<Image Source="{Binding ImageResourceKey, Converter={StaticResource BindingToStaticResourceConverter}}"></Image>
Code: Select all
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<BitmapImage x:Key="LogosAtlasDictionary" UriSource="LogosAtlasDictionary.png"/>
<CroppedBitmap x:Key="LogoOne" Source="{StaticResource LogosAtlasDictionary}" SourceRect="1,1,2889,409"/>
<CroppedBitmap x:Key="LogoTwo" Source="{StaticResource LogosAtlasDictionary}" SourceRect="2892,1,1078,152"/>
</ResourceDictionary>
Code: Select all
class BindingToStaticResourceConverter : IValueConverter
{
static FrameworkElement frameworkElement = new FrameworkElement();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return frameworkElement.FindResource(value as string);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
How should I handle this approach?