How to design parts of the application in XAML and how to reusing it then?

后端 未结 1 495
广开言路
广开言路 2021-01-26 10:58

I\'m working on a main window in my application and I would like to design parts of my window separately in Visual Studio designer.

Main window

  • Game desk
相关标签:
1条回答
  • 2021-01-26 11:22

    ItemTemplates or UserControls are probably what you are looking for.

    You can create ItemTemplates for things in a collection so they automatically get displayed one way or another and you can bind directly to the data in the class that is being represented by your ItemTemplate.

    Often, I like to create a new UserControl instead. You basically create a new control with XAML. Then you can create instances and set the datacontext of each as you stated you'd like to do.

    You can even use it in other XAML projects. Just be sure to add the namespace.Something like:

    xmlns:lp="clr-namespace:LocalProject"
    

    Then use it just like your other controls:

    <StackPanel>
        <lp:YourUserControl DataContext="bind to an object of the correct type here" />
    </StackPanel>
    

    And in the code behind you'll be able to access the bound DataContext object:

    YourCustomClass cc = this.DataContext as YourCustomClass;
    
    0 讨论(0)
提交回复
热议问题