Silverlight - Setting DataContext in XAML rather than in constructor?

后端 未结 7 597
心在旅途
心在旅途 2020-12-01 05:42

How can I set the DataContext on my Grid in XAML, instead of in the constructor?

Here is how I do it in the constructor (LayoutRoot is the XAML Grid defined in the X

7条回答
  •  鱼传尺愫
    2020-12-01 05:46

    In Silverlight 4, I was able to get this working by doing the following:

    Give the Page/UserControl an x:Name="myPage"

    In your control binding use normal Element bidning syntax. In my case I want to bind to an observable collection of objects in my code behind for my ItemsSource property:

    I haven't tried this with DataContext but know you can do element to element binding for DataContext as I do this for Grids whose context is based on the selected item of some other drop down on the page.

提交回复
热议问题