将函数更改为dependencyproperty

时间:2011-03-15 21:02:48

标签: c# xaml dependency-properties

我是XAML和WPF的新手,我正在学习DependencyProperty和Path。例如,我有一个像这样的函数

public byte[] DownloadPicture()
{
    WebClient webClient = new WebClient();
    byte[] data;
    data = webClient.DownloadData("https://graph.facebook.com/4/picture&type=large");       
    return data;
}

我有像这样的依赖属性

public static DependencyProperty DownloadPicProperty = 
DependencyProperty.Register("DownloadPic", typeof(byte), 
    typeof(ImageControl), new PropertyMetadata(false));        

如何将DependencyProperty与我编写的DownloadPicture函数连接?有什么建议?我应该在CLR包装器中写什么?

1 个答案:

答案 0 :(得分:0)

您也可以通过向控件添加标准属性来获取和设置依赖项属性的值。

public static DependencyProperty DownloadPicProperty =
    DependencyProperty.Register("DownloadPic", typeof(byte[]), typeof(ImageControl));

public byte[] DownloadPic
{
    get { return (byte[])GetValue(DownloadPicProperty); }
    set { SetValue(DownloadPicProperty, value); }
}

...
ImageControl imageControl = ...;
imageControl.DownloadPic = DownloadPicture();