如何在XAML中设计应用程序的部分以及如何重用? [英] How to design parts of the application in XAML and how to reusing it then?

查看:196
本文介绍了如何在XAML中设计应用程序的部分以及如何重用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的应用程序的主窗口中工作,我想在Visual Studio设计器中单独设计窗口的部分。



主窗口




  • 游戏桌(实际上更多的是它,设计游戏桌,将其标注为资源,然后只需通过简单的代码(如创建新对象和设置DataContext)创建它。

  • 控制台

  • 等等



VS有可能做这件事吗?



我只需要知道可以查找的内容,我不需要一个完整的解决方案。



谢谢你的建议!

解决方案

ItemTemplates或UserControls可能是您正在寻找的。

您可以为集合中的东西创建ItemTemplate,以便自动以某种方式显示,您可以直接绑定到由您的ItemTemplate表示的类中的数据。



通常,我喜欢创建一个新的UserControl。
你基本上创建一个新的控件与XAML。
然后,您可以创建实例并设置每个您想要做的每个的数据标签。



您甚至可以在其他XAML项目中使用它。只要确保添加命名空间,就像:

  xmlns:lp =clr-namespace:LocalProject

然后像其他控件一样使用它:

 <&的StackPanel GT; 
< lp:YourUserControl DataContext =绑定到此处正确类型的对象/>
< / StackPanel>

而在代码背后,您将可以访问绑定的DataContext对象:

  YourCustomClass cc = this.DataContext为YourCustomClass; 


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 (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;

这篇关于如何在XAML中设计应用程序的部分以及如何重用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆