Binding Silverlight UserControl custom properties to its' elements

后端 未结 6 654
长情又很酷
长情又很酷 2021-02-05 09:59

I\'m trying to make a simple crossword puzzle game in Silverlight 2.0. I\'m working on a UserControl-ish component that represents a square in the puzzle. I\'m having trouble wi

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-05 10:40

    First, set the DataContext on the UserControl using {RelativeSource Self}:

    
    

    Now you can bind the individual elements to the properties of the usercontrol:

      
    
    

    For SL 2.0, you'll need to set the DataContext on the UserControl's Loaded event handler.

    private void UserControl_Loaded( object sender, RoutedEventArgs e ) {
        LayoutRoot.DataContext = this;
    }
    

提交回复
热议问题