两个窗口上的UI相同,但每个窗口的功能不同

时间:2016-02-05 03:08:50

标签: c# wpf xaml

WPF应用

我将在两个窗口中显示identical UI

假设我们通过相同的字段跟随获胜name,address,dob,phoneNo etc.用于添加

  1. 客户详情
  2. 员工详情
  3.   
        

    有没有办法可以构建single UI并在两个不同的窗口上使用它并编写logic per window

      

    我是 WPF 的新手,我搜索过它,在同一窗口找到用于navigation页面 UserControl < / strong>用于自定义控制作为日历等。我认为这不是我想要的..

    相信我,我的申请中有很多identical UI,所以我需要解决它..

      

    那么可能还是有另一种好方法?

         

    或者不可能,我必须复制并粘贴xaml :( ??

    谢谢。

3 个答案:

答案 0 :(得分:1)

您可以使用MVVM设计模式。如果您有两个单独的类对象,例如Customer和Staff,但对象中的名称字段/属性相同,则可以使用控件的绑定属性填充字段和Windows的DataContext属性以将对象设置为数据源。这里还有一个优点,就是您不需要从控件中编写set / get值。由于绑定属性双向工作,如果对象值发生更改,它将在UI上更新,如果值在UI上由USer手动修改,它将立即反映在类对象中。

答案 1 :(得分:0)

如果要在应用程序的不同页面或窗口之间共享UI代码,则需要创建包含XAML的UserControl,并将可配置设置公开为DependancyProperty和操作作为EventCommand&#39; s。

重新。 &#34;我是WPF的新手&#34;:它与你在WinForms中执行此操作的过程相同,创建一个自定义控件,可以执行您想要的操作并重新执行操作 - 在多个地方使用它。

答案 2 :(得分:0)

我认为,这是一个 - 希望 - 你想要的简单例子。我假设你的UI是相同的,他们被绑定到不同的模型或ViewModel。我先创建UserControl

<UserControl x:Class="UserControlDemo.MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:UserControlDemo"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             Loaded="UserControl_Loaded">
    <Grid>
        <TextBlock VerticalAlignment="Center" Width="200" Text="{Binding Path=objName}"/>
    </Grid>
</UserControl>

它只包含一个名为TextBlock的{​​{1}} Binding,并注意到我正在使用objName事件。

代码隐藏:

Loaded

好的,让我们看看我们在这里得到了什么。我有两个依赖属性,public partial class MyUserControl : UserControl { public string customObject { get { return (string)GetValue(customObjectProperty); } set { SetValue(customObjectProperty, value); } } public static readonly DependencyProperty customObjectProperty = DependencyProperty.Register("customObject", typeof(string), typeof(MyUserControl), null); public string objName { get { return (string)GetValue(objNameProperty); } set { SetValue(objNameProperty, value); } } public static readonly DependencyProperty objNameProperty = DependencyProperty.Register("objName", typeof(string), typeof(MyUserControl), null); public MyUserControl() { InitializeComponent(); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { var obj = CustomObjectFactory.GetCustomObject(customObject); objName = obj.GetTypeName(); DataContext = this; } customObject。它们是依赖属性的事实提供了将它们绑定到UI中的控件的功能。我们将objName绑定到上面UserControl中objName的{​​{1}}属性。

Text事件中,我使用工厂方法返回正确的对象类型。见下文。

然而,TextBlock属性是在控件初始化期间设置的,但要看到我们必须转到MainWindow.xaml(或将使用UserControl的窗口):

Loaded

正如您所看到的,MainWindow.xaml并不是非常拥有&#34;拥有&#34;代码只有customObject<Window x:Class="UserControlDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:UserControlDemo" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <TabControl> <TabItem Header="Tab 1"> <local:MyUserControl customObject="Object 1"/> </TabItem> <TabItem Header="Tab 2"> <local:MyUserControl customObject="Object 2"/> </TabItem> </TabControl> </Grid> </Window> 和两个Grid。在每个TabItems中都有对UserControl的引用,但请注意,因为我们在TabControl中有一个名为TabItem的依赖项属性,所以我可以将值传递给MyUserControl。

然后,该值与customObject方法和MyUserControl一起使用,以根据我所在的TabItem加载两种不同的类型。

下面,我创建了两个类;实现Factory接口的InterfaceObject1

Object2

如您所见,这些类有一个方法只返回字符串中的类名。

为了能够动态返回正确的对象类型,具体取决于您当前所处的窗口,我们必须使用ICustomObject

public class Object1 : ICustomObject
{
    public string GetTypeName()
    {
        return this.GetType().ToString();
    }
}

public class Object2 : ICustomObject
{
    public string GetTypeName()
    {
        return this.GetType().ToString();
    }
}

结果是当我打开Tab 1时,我可以看到带有文本&#34; UserControlDemo.Object1&#34;的TextBlock。在Tab 2中,我看到&#34; UserControlDemo.Object2&#34;

这意味着我可以添加另一个TabItem,并且很容易在我的UI中添加另一个类型来显示而不会更改我的代码。

我希望这对你有所帮助。