I can't get the following to work, I must be missing something elementary.
My objective is to have a style on a dialog (Window) to set an image on a button within it.
So what I have is in the dialog code I added a DependencyProperty like so:
public static readonly DependencyProperty ImageRefreshProperty =
DependencyProperty.Register(nameof(ImageRefreshProperty), typeof(ImageSource),
typeof(MyDlg), new PropertyMetadata(new BitmapImage(
new Uri(@"pack://application:,,,/component/Resources/refresh.png"))));
public ImageSource ImageRefresh {
get { return (ImageSource)GetValue(ImageRefreshProperty); }
set { SetValue(ImageRefreshProperty, value); }
}
In Xaml I have this:
<Button DockPanel.Dock="Left" Style="{StaticResource buttonIcon}">
<Image Source="{Binding Path=ImageRefresh,
RelativeSource={RelativeSource AncestorType=local:MyDlg}}" />
</Button>
This works fine as long as I use code to change the image, like
dlg.ImageRefresh = new BitmapImage(
new Uri("pack://application:,,,/component/Resources/refr.png"));
But ideally I would like to set the image through a style, in a way like this:
<Style TargetType="{x:Type MyDlg}">
<Setter Property="ImageRefresh" Value="pack://application:,,,/component/Resources/refr.png" />
</Style>
The error I am getting is:
System.Windows.Markup.XamlParseException:
''Provide value on 'System.Windows.StaticResourceExtension' threw an exception.'
Inner Exception System.Windows.Markup.XamlParseException:
ArgumentNullException: Key cannot be null. Arg_ParamName_Name
I have also tried a Setter Value that defines a BitmapImage within it instead of a string, but I still get the same error:
<Setter Property="ImageRefresh">
<Setter.Value>
<BitmapImage UriSource="pack://application:,,,/component/Resources/refr.png"/>
</Setter.Value>
</Setter>
Setting a break point to the getter/setter of the DependencyProperty does not even get hit, nor does a Converter on the binding of the Source of the Image.
What am I missing here?
Edit: to see if the ImageSource has anything to do with it, I tested my code with another property of type bool? to set a button's IsEnabled property in the dialog, but the result is the same. So the error is not with the Image, its pack URL (which works by the way) but apparently with something else.
question from:
https://stackoverflow.com/questions/65895552/style-to-change-images-on-buttons-in-dialog