Why ImageBrush raises an error but still shows up normally?

587 views Asked by At

I have a custom control with the following DependencyPorperty:

  public static readonly DependencyProperty MyImageProperty = DependencyProperty.Register(
      "MyImage",
      typeof(ImageSource),
      typeof(ImageButton), // that's my custom control class name
      new PropertyMetadata(null));

  public ImageSource MyImage
  {
      get { return (ImageSource)GetValue(MyImageProperty); }
      set { SetValue(MyImageProperty, value); }
  }

If I try the following code:

        <Border Name="btnBorder" Height="30">
                <Border.Background>
                    <ImageBrush ImageSource="{Binding Path=MyImage}"  />
                </Border.Background>
        </Border>

I always have the following error in the Visual Studio 2010 Output window:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=MyImage; DataItem=null; target element is 'ImageBrush' (HashCode=47992654); target property is 'ImageSource' (type 'ImageSource')

but still the application runs fine and the image is loaded normally, no exceptions.

But if I do the following:

<Image Name="btnImage" Source="{Binding MyImage}" />

there are no errors and again application works ok.

Why is this error for ImageBrush? Am I doing something wrong with it?

0

There are 0 answers