c# - Modern UI for WPF - ModernDialog: Allow to click Ok only on some conditions -


i started new small project, using modernui wpf.

i've display dialog 2 textbox , 2 button(ok/cancel).

the 2 textbox have validation(one name should >0, other should email).

currently i've following:

        var usereditionform = new usereditionform(oneuserconfigurationviewmodel);         var dlg = new moderndialog         {             title = "user edition",             content = usereditionform,             width = 300         };         dlg.buttons = new[] {dlg.okbutton, dlg.cancelbutton};         dlg.showdialog();         return dlg.dialogresult.hasvalue && dlg.dialogresult.value; 

the content user control is:

<usercontrol x:class="test.usercontrols.usereditionform"              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"              mc:ignorable="d"              d:designheight="300" d:designwidth="300">     <grid>         <stackpanel orientation="vertical">             <stackpanel.resources>                 <style targettype="stackpanel">                     <setter property="orientation" value="horizontal" />                     <setter property="margin" value="0,0,0,4" />                 </style>                 <style targettype="label" basedon="{staticresource {x:type label}}">                     <setter property="width" value="100" />                     <setter property="verticalalignment" value="center" />                 </style>             </stackpanel.resources>              <stackpanel>                 <label content="name" target="{binding elementname=textfirstname}" />                 <textbox x:name="textfirstname" width="150"                          text="{binding user.name, relativesource={relativesource ancestortype=usercontrol}, mode=twoway, validatesondataerrors=true}" />             </stackpanel>             <stackpanel>                 <label content="email" target="{binding elementname=textemail}" />                 <textbox x:name="textemail" width="150"                          text="{binding user.email, relativesource={relativesource ancestortype=usercontrol}, mode=twoway, validatesondataerrors=true}" />             </stackpanel>         </stackpanel>     </grid> </usercontrol> 

the validation made, prevent user click "ok" until validation good. possible? if yes, how?

this pretty tricky worked me:

viewmodel

public class userinfo : inotifypropertychanged, idataerrorinfo {     private static regex emailregex = new regex(@"^\w+(?:\.\w+)*?@[a-za-z_]+?\.[a-za-z]{2,3}$");      private string firstname;     private string email;      public string firstname     {         { return firstname; }         set { firstname = value; onpropertychanged(); }     }      public string email     {         { return email; }         set { email = value; onpropertychanged(); }     }      public string error     {         { return null; }     }      public string this[string columnname]     {                 {             switch (columnname)             {                 case "firstname":                     return string.isnullorwhitespace(firstname) ? "firstname required!" : null;                 case "email":                     return email == null || !emailregex.ismatch(email) ? "the email address not valid!" : null;                 default:                     return null;             }         }     }      public logincommand logincommand { get; private set; }      public userinfo()     {         logincommand = new logincommand(this);     }      private void onpropertychanged([callermembername]string propertyname = "")     {         propertychanged(this, new propertychangedeventargs(propertyname));     }      public event propertychangedeventhandler propertychanged = delegate { }; } 

command (no clue want call it...)

public class logincommand : icommand {     private userinfo info;     private icommand attachedcommand;      public bool canexecute(object parameter)     {         return parameter != null && info["firstname"] == null && info["email"] == null;     }      public event eventhandler canexecutechanged = delegate { };      public void execute(object parameter)     {         debug.writeline("this works!");          //add execution logic here          if (attachedcommand != null)         {             attachedcommand.execute(parameter); //should close window         }      }      private void info_propertychanged(object sender, propertychangedeventargs e)     {         canexecutechanged(this, new eventargs());     }      public logincommand(userinfo info)     {         this.info = info;         info.propertychanged += info_propertychanged;     }      public void attachcommand(icommand command) //attach original command here     {         attachedcommand = command;     } } 

usage modernui

userinfo info = new userinfo(); userinfoform form = new userinfoform(info); moderndialog dialog = new moderndialog {     title = "user edition",     content = form,     width = 300 }; button btnok = dialog.okbutton; icommand originalcommand = btnok.command; //the original command should close window keep btnok.command = info.logincommand; info.logincommand.attachcommand(originalcommand); //and attach command dialog.buttons = new[] { btnok, dialog.cancelbutton }; dialog.showdialog(); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -