如何使用命名空间将一个WPF应用程序中多个.xaml文件的后备字段拆分为多个类? [英] How might I use namespaces to split the backing fields for multiple .xaml files in one WPF application into multiple classes?

查看:139
本文介绍了如何使用命名空间将一个WPF应用程序中多个.xaml文件的后备字段拆分为多个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用包含数十个.xaml文件,以及数十个xaml字段,这些字段又绑定到许多支持属性.现在,这是 所有的平面命名空间.  

I have an app with scores of .xaml files, and dozens of xaml fields that are in turn bound to dozens of backing properties.  Right now, this is all a flat namespace.  

每个xaml代表一个"UserControl"(一个UserControl)每个xaml). 

一个问题是,每个支持属性(数十个)都生活在单个对象中. ;我想使用特定的类来控制特定的后备属性.例如,我可能 希望将管理"页面的xaml字段全部绑定到Admin对象,而状态"区域字段可能绑定到状态"对象.

A problem is that every backing property (dozens) lives in a single object.  I want to control specific backing properties using specific classes.  For instance, I might want the Admin page xaml fields to all be bound to an Admin object while the Status area fields might be bound to a Status object.  

我该怎么做?     现在,我只能选择将每个字段都绑定 xaml到单个名称空间.

How can I do this?    Right now, I only have the option of binding every field in every xaml to a single name space.

例如,我想要以下内容:

For instance, I want something like:

文件:UC_AdminView.xaml:  ---将此处的字段绑定到  ; AdminView 支持对象

File: UC_AdminView.xaml:  --- bind the fields here to the AdminView backing object

<UserControl x:Class="Gui.View.UC_AdminView" … > <StackPanel x:Name="AppInfo"> <Label Content="{Binding AdminView.AppName}" … />

</StackPanel> …

</StackPanel> .…

-和-

文件:UC_StatusArea.xaml:  ---将此处的字段绑定到  ; StatusArea  支持对象 

File: UC_StatusArea.xaml:  --- bind the fields here to the StatusArea backing object  

<UserControl x:Class="Gui.View.UC_StatusArea" … > <StackPanel x:Name="StatusStuff"> <Label Content="{Binding StatusArea.Name}" …

/>

</StackPanel> …

</StackPanel> …


推荐答案

不知道要用到什么,但NameSpace不是您想要的.  名称空间只是在多个名称空间中允许使用相同名称的一种方法.

Not sure where you are going with this but NameSpace is not what you want.  A NameSpace is simply a method of allowing the same name in mulitple NameSpaces.

例如,TextBox存在于多个NameSpace(System.Windows.Controls,System.Windows.Forms等)中.  

For example TextBox exists in multiple NameSpaces (System.Windows.Controls, System.Windows.Forms and more).  

对于绑定,您使用的是DataContext,它是一个对象,其中包含您绑定到控件的属性.您可以将DataContext设置为任何对象,只要它包含所需的属性即可(实际上,如果 他们不存在).

For binding you are using DataContext which is an object which contains the properties which you binding to the controls.  You could set the DataContext to any object as long as it contains the needed properties (actually WPF will fail elegantly if they are not present).


这篇关于如何使用命名空间将一个WPF应用程序中多个.xaml文件的后备字段拆分为多个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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