Answer the question
In order to leave comments, you need to log in
How to bind a VM property to a "ResourceKey" using the DynamicResource extension?
I have icon resources for buttons, icons are represented as XAML Resource. I need to bind the IconBrush property from the view model to the DynamicResource extension key.
<Button
Grid.Row="2"
Grid.Column="1"
Width="48"
BorderBrush="{x:Null}"
Command="{Binding OpenDialogIcons}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}}"
Style="{StaticResource BubleButton}">
<Button.Content>
<!-- Данная привязка не работает, как можно решить проблему? -->
<DynamicResource ResourceKey="{Binding IconBrush.ResourceKey, Mode=TwoWay}"/>
</Button.Content>
</Button>
public DynamicResourceExtension IconBrush
{
get { return _iconBrush; }
set
{
SetProperty(ref _iconBrush, value, () => IconBrush);
}
}
IconResources resources = new IconResources();
IconBrush = resources.DrawIcon();
public DynamicResourceExtension DrawIcon()
{
ResourceDictionary resource = new ResourceDictionary();
resource.Source = new Uri("../../Resources/Icons.xaml", UriKind.Relative);
DynamicResourceExtension dynamicResource = new DynamicResourceExtension();
foreach (var key in resource.Keys)
{
if ((string) key == "SettingIcon")
dynamicResource.ResourceKey = key;
}
return dynamicResource;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question