在代码中创建绑定(WinRT)

Igo*_*man 4 c# xaml windows-8 windows-runtime

我有以下课程

ImageViewModel: INotifyPropertyChanged
{ ...
   String Url;
}

AdViewModel: INotifyPropertyChanged
{ ...
    ImageViewModel Image
}
Run Code Online (Sandbox Code Playgroud)

AdvisionModel perodicaly更改了Image属性(动画广告).

当我有以下XAML时:

<Grid>
  <Image Source="{Binding Image.Url}"
           Width="{Binding Image.Width}"
           Height="{Binding Image.Height}" />
Run Code Online (Sandbox Code Playgroud)

并将Grids DataContext设置为AdViewModel的实例,一切都按预期工作.但我需要在C#代码中创建XAML以在其他地方使用它.创建网格并将图像作为其子项附加很容易,但如何创建绑定?

Mak*_*kau 8

尝试一些事情

AdViewModel vm = new AdViewModel;      
Binding binding = new Binding
{
    Path = new PropertyPath("Width"),
    Source = vm.Image
};
nameOfGridInXaml.SetBinding(Image.WidthProperty, binding);
Run Code Online (Sandbox Code Playgroud)