跨多个 Xaml 文件拆分 WPF 接口 [英] Splitting WPF interface across multiple Xaml files

查看:34
本文介绍了跨多个 Xaml 文件拆分 WPF 接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 XAML 创建用户界面.但是,该文件很快变得非常大且难以处理.将其拆分为多个文件的最佳方法是什么.

I am trying to create a user interface using XAML. However, the file is quickly becoming very large and difficult to work with. What is the best way for splitting it across several files.

我希望能够将 ComboBox 等元素的内容设置为在不同 xaml 文件(但在同一个 VS 项目中)中定义的元素.

I would like to be able to set the content of an element such as a ComboBox to an element that is defined in a different xaml file (but in the same VS project).

谢谢

推荐答案

您可以通过定义 UserControl 来拆分大型用户界面.

You can split a large user interface by defining UserControls.

右键单击解决方案树,选择添加"->新建项目...",然后选择用户控制".您可以按照正常方式进行设计.

Right-click on the solution tree, choose Add->New Item... then User Control. You can design this in the normal way.

然后,您可以使用命名空间声明在 XAML 中引用您的用户控件.假设您想将 UserControl 包含在 Window 中.在以下示例中,我将名为Foo"的 UserControl 添加到命名空间YourCompany.Controls":

You can then reference your usercontrol in XAML using a namespace declaration. Let's say you want to include your UserControl in a Window. In the following example I've added a UserControl named "Foo" to the namespace "YourCompany.Controls":

<Window x:Class="YourCompany.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls="clr-namespace:YourCompany.Controls">

  <Controls:Foo ... />

对于您的特定示例,您将通过定义一个显示用户控件中数据的 DataTemplate 来使用组合框中的用户控件.

For your specific example, you would make use of your usercontrol in a combobox by defining a DataTemplate that displayed the data within your usercontrol.

这篇关于跨多个 Xaml 文件拆分 WPF 接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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