c# - UpdateSourceTrigger not working? -
i'm trying validate text in textbox when key pressed. here's shortest code sample shows i'm trying do:
<window x:class="wpfapplication1.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <grid> <textbox fontsize="15" horizontalalignment="left" name="txtemail" verticalalignment="top" width="135" text="{binding validationrules.emailaddress, validatesonexceptions=true, updatesourcetrigger=propertychanged}"/> </grid> </window> "validationrules" class:
class validationrules { string email = ""; public string emailaddress { { homecoming email; } set { console.writeline("setting!"); //only check if there text now... if (string.isnullorwhitespace(value)) { throw new exception(); } email = value; } } } when start typing in textbox, don't "setting" console output, though i'm using updatesourcetrigger=propertychanged. i've done research examples find long winding , confusing. appreciate if point out other mistakes have in validation, seek explain in simple terms if possible because i'm new wpf.
it must issue on setting datacontext.
this illustration seems work fine:
code:
public partial class mainwindow : window, inotifypropertychanged { public mainwindow() { initializecomponent(); validationrules = new validationrules(); } private validationrules _validation; public validationrules validationrules { { homecoming _validation; } set { _validation = value; notifypropertychanged("validationrules"); } } public event propertychangedeventhandler propertychanged; private void notifypropertychanged(string property) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(property)); } } } public class validationrules : inotifypropertychanged { string email = ""; public string emailaddress { { homecoming email; } set { console.writeline("setting!"); //only check if there text now... if (string.isnullorwhitespace(value)) { throw new exception(); } email = value; notifypropertychanged("emailaddress"); } } public event propertychangedeventhandler propertychanged; private void notifypropertychanged(string property) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(property)); } } } xaml
<window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:class="wpfapplication1.mainwindow" title="mainwindow" height="125.078" width="236.441" x:name="ui" > <grid datacontext="{binding elementname=ui}"> <textbox fontsize="15" horizontalalignment="left" name="txtemail" verticalalignment="top" width="135" text="{binding validationrules.emailaddress, validatesonexceptions=true, updatesourcetrigger=propertychanged}"/> </grid> </window> c# wpf
No comments:
Post a Comment