Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat design pattern should I use to make such dialogs?
    text
    copied!<p>I want to develop dialog for editing objects that make use of polymorphism. Currently I'm using this pattern:</p> <p><strong>MyObject.cs:</strong></p> <pre><code>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; } } } </code></pre> <p><strong>MainWindow.xaml:</strong></p> <pre><code>&lt;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"&gt; &lt;StackPanel Margin="20"&gt; &lt;TextBlock Text="Title" /&gt; &lt;TextBox Name="txtTitle" /&gt; &lt;RadioButton Name="rdBoolSettings" Content="BoolSettings" IsChecked="True" Margin="0, 20, 0, 0" /&gt; &lt;CheckBox Name="chBool" Content="True" Margin="20, 0, 0, 20" /&gt; &lt;RadioButton Name="rdStringSettings" Content="StringSettings" /&gt; &lt;TextBox Name="txtString" Margin="20, 0, 0, 20"/&gt; &lt;Button Content="OK" Click="OK_click" /&gt; &lt;Button Content="Cancel" Click="Cancel_click" Margin="0, 10" /&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p><strong>MainWindow.xaml.cs:</strong></p> <pre><code>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; } } } </code></pre> <p><strong>ExternalCode:</strong></p> <pre><code>var f = new EditMyObjectDialog(myObject); if (f.ShowDialog() == true) myObject = f.Result; </code></pre> <p>I belive there is much better design pattern that uses data binding etc. So basically I have two questions. </p> <ol> <li>How to make data binding not to modify object until user hits 'OK'? </li> <li>How to correctly handle 'Settings' property? What to do when user switches setting's type?</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload