0

My app has switch-backgrounds option, so I'd like to check whether my Mainwindow's Background's ImageSource is nulled (then I'll set it to the default Background), the problem is I can't reach it through this.Background.ImageSource, because there's no such option. Any ideas?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Yair V.
  • 109
  • 1
  • 10
  • Possible duplicate of [How to hit detect on a null or transparent background](http://stackoverflow.com/questions/15169358/how-to-hit-detect-on-a-null-or-transparent-background) – Damian Galletini Jan 25 '17 at 16:04
  • Excuse me but this is code-behind question, not XAML. – Yair V. Jan 25 '17 at 16:16

1 Answers1

1

You could cast the Background to an ImageBrush using the as operator and check whether you got a null reference back:

ImageBrush brush = this.Background as ImageBrush;
if(brush == null || brush.ImageSource == null)
{
    //no ImageBrush set as the Background...
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • 1
    If the Background property has been set to an ImageBrush the cast will succeed, i.e. you will get a reference to the ImageBrush back, and if it hasn't you will get a null reference back. It's as simple as that: https://msdn.microsoft.com/en-us/library/cscsdfbt.aspx – mm8 Jan 25 '17 at 16:51