我应该使用哪种设计模式进行此类对话框? [英] What design pattern should I use to make such dialogs?

查看:86
本文介绍了我应该使用哪种设计模式进行此类对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个对话框来编辑利用多态性的对象。目前,我正在使用以下模式:

I want to develop dialog for editing objects that make use of polymorphism. Currently I'm using this pattern:

MyObject.cs:

using System;

namespace WpfApplication3
{
    public class MyObject
    {
        public string Title { get; set; }
        public MySettings Settings { get; set; }
    }

    public abstract class MySettings
    {
        public abstract string GetSettingsString();
    }

    public class MyBoolSettings : MySettings
    {
        public bool BoolSetting { get; set; }

        public override string GetSettingsString()
        {
            return "BoolSetting = " + BoolSetting;
        }
    }

    public class MyStringSettings : MySettings
    {
        public string StringSetting { get; set; }

        public override string GetSettingsString()
        {
            return "StringSetting = " + StringSetting;
        }
    }
}

MainWindow.xaml :

<Window x:Class="WpfApplication3.EditMyObjectDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="EditMyObjectDialog" Height="350" Width="350">
    <StackPanel Margin="20">
        <TextBlock Text="Title" />
        <TextBox Name="txtTitle" />
        <RadioButton Name="rdBoolSettings" Content="BoolSettings" IsChecked="True" Margin="0, 20, 0, 0" />
        <CheckBox Name="chBool" Content="True" Margin="20, 0, 0, 20" />
        <RadioButton Name="rdStringSettings" Content="StringSettings" />
        <TextBox Name="txtString" Margin="20, 0, 0, 20"/>
        <Button Content="OK" Click="OK_click" />
        <Button Content="Cancel" Click="Cancel_click" Margin="0, 10" />
    </StackPanel>
</Window>

MainWindow.xaml.cs:

using System.Windows;

namespace WpfApplication3
{
    public partial class EditMyObjectDialog : Window
    {
        public MyObject Result { get; set; }

        public EditMyObjectDialog(MyObject objectToEdit)
        {
            InitializeComponent();
            txtTitle.Text = objectToEdit.Title;
            if (objectToEdit.Settings is MyBoolSettings)
            {
                rdBoolSettings.IsChecked = true;
                chBool.IsChecked = (objectToEdit.Settings as MyBoolSettings).BoolSetting;
            }
            if (objectToEdit.Settings is MyStringSettings)
            {
                rdBoolSettings.IsChecked = true;
                txtString.Text = (objectToEdit.Settings as MyStringSettings).StringSetting;
            }
        }

        private void OK_click(object sender, RoutedEventArgs e)
        {
            Result = new MyObject() { Title = txtTitle.Text };
            if (rdBoolSettings.IsChecked == true) 
                Result.Settings = new MyBoolSettings() { BoolSetting = chBool.IsChecked == true };
            if (rdStringSettings.IsChecked == true)
                Result.Settings = new MyStringSettings() { StringSetting = txtString.Text };
            DialogResult = true;
        }

        private void Cancel_click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }
    }
}

外部代码:

var f = new EditMyObjectDialog(myObject);
if (f.ShowDialog() == true)
    myObject = f.Result;

我相信有更好的设计模式可以使用数据绑定等。所以基本上我有两个问题。

I belive there is much better design pattern that uses data binding etc. So basically I have two questions.


  1. 如何在用户单击确定之前,不使数据绑定到
    修改对象?

  2. 如何正确处理设置的
    属性?用户
    切换设置类型时该怎么办?


推荐答案

我相信你寻找的是DataBinding和DataTemplating的组合。 DataTemplating将允许您为不同的业务对象定义不同的视觉元素(在这种情况下, MyBooleanSettings MyStringSettings 。DataBinding将允许视觉元素来更新和更新业务对象中的数据。

What I believe you're looking for is a combination of DataBinding and DataTemplating. DataTemplating will allow you to define different visual elements for different business objects (in this case MyBooleanSettings and MyStringSettings. DataBinding will allow the visual elements to update and be updated my the data in the business objects.

示例(xaml):

<Window DataContext={Binding RelativeSource={RelativeSource Self}}">
    <Window.Resources>
        <DataTemplate DataType={x:Type local:MyObject}">
            <TextBlock Text={Binding Title}" />
            <ContentPresenter Content="{Binding Settings}" />
        </DataTemplate>
        <DataTemplate DataType={x:Type local:MyObject}">
            <TextBox Text={Binding 
        </DataTemplate>
        <DataTemplate DataType={x:Type local:MyBoolSettings}>
            <CheckBox IsChecked="{Binding BoolSetting}" />
        </DataTemplate>
        <DataTemplate DataType={x:Type local:MyStringSettings}>
            <TextBox Text="{Binding StringSetting}" />
        </DataTemplate>
    </Window.Resources>
    <ContentPresenter Content="{Binding ObjectToEdit}" />
</Window>

然后在后面的代码中定义:

Then in the code behind define:

public MyObject ObjectToEdit { get; set; }

最后更新您的对象:

public class MySettings : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(sting s)
    {
        if(PropertyChanged != null)
        {
            PropertyChanged(s);
        }
    }
}

public class BoolSettings : MySettings
{
    bool _value;

    bool BoolSetting
    {
        get { return _value; }
        set
        {
            if(_value != value)
            {
                _value = value;
                OnPropertyChanged("BoolSetting");
            }
         }
    }
}

但是,如果您确实需要控制视图和对象的同步时间,则应在相应的绑定上使用 UpdateSourceTrigger 属性。

If however you really need to control when the view and object sync you should use the UpdateSourceTrigger property on the corresponding bindings.

如果您还想阅读其他文章,我建议: http: //msdn.microsoft.com/zh-CN/library/ms752347.aspx

If you want some additional reading I recommend: http://msdn.microsoft.com/en-us/library/ms752347.aspx

这篇关于我应该使用哪种设计模式进行此类对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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