Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

wpf - Style to change images on buttons in dialog

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found the answer myself after some digging around in my code. The problem was the definition of the DependencyProperty. My code had:

public static readonly DependencyProperty ImageRefreshProperty =
  DependencyProperty.Register(nameof(ImageRefreshProperty) /*Wrong*/,
    typeof(ImageSource),
    typeof(MyDlg), new PropertyMetadata(new BitmapImage(
      new Uri(@"pack://application:,,,/component/Resources/refresh.png"))));

but it should be:

public static readonly DependencyProperty ImageRefreshProperty =
  DependencyProperty.Register(nameof(ImageRefresh) /*Correct*/,
    typeof(ImageSource),
    typeof(MyDlg), new PropertyMetadata(new BitmapImage(
      new Uri(@"pack://application:,,,/component/Resources/refresh.png"))));

A very small oversight with big consequences...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...