Binding Silverlight UserControl custom properties to its' elements

后端 未结 6 598
长情又很酷
长情又很酷 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:45

    I may not be understanding your issue exactly. In Silverlight, you are able to bind to almost any data object. So, if you have a PuzzleSquare class that contains properties Content and Label, you may bind to these properties directly from the object.

    Let's say you created a simple object PuzzleSquare:

        public class PuzzleSquare
        {
          public string Content{ get; set; }
          public string Label{ get; set; }
    
          public void PuzzleSquare(){};
          public void PuzzleSquare(string label, string content):this()
          {
             Content = content;
             Label = label;
          }    
        }
    

    So, if you are building the app with the classic view/code behind model, your code behind would add this object to the DataContext property of the grid on page load:

    LayoutRoot.DataContext = new PuzzleSquare("1", "A");
    

    Your Xaml would bind to the Square property:

                    
        
    

    Does that make sense?

    ib.

提交回复
热议问题