开发者

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

I'm working on a main window in my applicat开发者_JAVA百科ion and I would like to design parts of my window separately in Visual Studio designer.

Main window

  • Game desk (actually more of them and therefore it would be nice to design the game desk, mark it as a resource and then just via simple code (something like creating a new object and setting DataContext) create it.
  • Console
  • And so on

Is it possible in VS to do this thing?

I just need to know what to look for if it is possible. I don't need a whole solution.

Thank you for suggestions!


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜