سؤال

I have a user WPF UserControl which is just a grid with an Image in it and I'm bidning the Image to a ImageSource Dependency Property named Source.

<UserControl x:Class="ImageOnlyClient.MyImage"
         x:Name="MyImageControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d"             
         d:DesignHeight="300" d:DesignWidth="300">
<Grid Name="MainGrid">
    <Border Name="MyImageBorder" BorderThickness="2" BorderBrush="Orange">
        <Image Name="MyImage" VerticalAlignment="Top" Opacity="1"
               RenderOptions.BitmapScalingMode="NearestNeighbor"
               Source="{Binding Path=Source, Mode=OneWay}" />
    </Border>
</Grid>

In the UserControl codebehind my class is defined as follows:

public partial class MyImage : UserControl
{
    public ImageSource Source
    {
        get { return (ImageSource)GetValue(SourceProperty); }
        set { SetValue(SourceProperty, value); }
    }    

    #region Source DependencyProperty
    public static readonly DependencyProperty SourceProperty;

    private static void SourceProperty_PropertyChanged(DependencyObject dobj, DependencyPropertyChangedEventArgs e)
    {
        //To be called whenever the DP is changed.            
        System.Diagnostics.Debug.WriteLine("SourceProperty changed is fired");         
    }

    private static bool SourceProperty_Validate(object Value)
    {
        //Custom validation block which takes in the value of DP
        //Returns true / false based on success / failure of the validation
        //MessageBox.Show(string.Format("DataValidation is Fired : Value {0}", Value));
        return true;
    }

    private static object SourceProperty_CoerceValue(DependencyObject dobj, object Value)
    {
        //called whenever dependency property value is reevaluated. The return value is the
        //latest value set to the dependency property
        //MessageBox.Show(string.Format("CoerceValue is fired : Value {0}", Value));
        return Value;
    }

    #endregion


    static MyImage()
    {
        SourceProperty =  DependencyProperty.Register("Source", typeof(ImageSource), typeof(MyImage),
            new FrameworkPropertyMetadata(null,
                                          FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Journal,
                                          new PropertyChangedCallback(SourceProperty_PropertyChanged),
                                          new CoerceValueCallback(SourceProperty_CoerceValue),
                                          false, UpdateSourceTrigger.PropertyChanged),
                                      new ValidateValueCallback(SourceProperty_Validate));

    }

    public MyImage()
    {
        InitializeComponent();
    }
}

In a Window I try to use the Image as follows and Bind it's source property to a WritableBitmap (MyClient.ImageMgr.ImageSource) which I can successfully bind to a regular Image control.

<local:MyImage x:Name="imgPrimaryImage" Height="768" Width="1024" Grid.Column="1" Grid.RowSpan="2"
                 Source="{Binding Path=MyClient.ImageMgr.ImageSource}" />

Any help on what's going on here would be greatly appreciated. I'm getting the following binding error:

System.Windows.Data Error: 40 : BindingExpression path error: 'Source' property not found on 'object' ''ImageOnly' (Name='')'. BindingExpression:Path=Source; DataItem='ImageOnly' (Name=''); target element is 'Image' (Name='MyImage'); target property is 'Source' (type 'ImageSource')

هل كانت مفيدة؟

المحلول

You're attempting to bind the Image's "Source" to a property on the parent UserControl, but if you don't specify a source (I mean a binding source ... the terminology here is confusing), then the runtime will look for the property on the default data context. I would infer from the error message that a class of type "ImageOnly" is the inherited data context in your user control.

You probably just want to specify a relative source, like this:

<Image ...
       Source="{Binding 
           RelativeSource={RelativeSource AncestorType=UserControl},
           Path=Source, 
           Mode=OneWay}" 
       />

نصائح أخرى

I finally got it to work @McGarnagle's suggestion worked, but in the mean time I had added a DataContext=this in the UserControl's constructor which was messing up the DataContext of the UserControl

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top