سؤال

I have my own control and the binding does not work in it. Below is the code that I tried to bind.

The code in XAML

<cc:MyControl Name="myControl" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemSource="{Binding Path=Document}"></cc:MyControl>

The code in CodeBehind for ItemSource Dependency Property

 public Object ItemSource
    {
        get { return (Object)GetValue(ItemSourceProperty); }
        set { SetValue(ItemSourceProperty, value); }
    }

public static readonly DependencyProperty ItemSourceProperty =
        DependencyProperty.Register("ItemSource", typeof(Object), typeof(MyControl), new PropertyMetadata(null, new PropertyChangedCallback(DocumentLoadCallBack)));

 private static void DocumentLoadCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
}

This does not gets even triggered.....

But When I provide ItemSource with some data without binding as below, the DocumentLoadCallBack gets triggered.

<cc:MyControl Name="myControl" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemSource="ImagePath"></cc:MyControl>

The "Document" bind property code

public sealed partial class MainPage : Page
{
private StorageFile _doc = null;
    public StorageFile Document
    {
        get
        {
            if (this._doc == null)
            {
                this._doc = GetDoc().Result;
            }
            return this._doc;
        }
        set
        {
            this._doc = value;
        }
    }

    private async Task<StorageFile> GetDoc()
    {
        //return imagedocument location
    }
}

Anyone, help me out on this?

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

المحلول

You are using async method inside of the get accesor of the property. So when you try to do get it wouldn't return a result because the method is async and you shouldn't do like that.

You should read first the file (Load event for example) then update the property and not read the file inside of the property. This isn't a very good dev practice.

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