c# - How to use FileSystemWatcher to refresh text being returned from converter -


bare me new c#. have built converter class pass in file path , returns actual text of file.

   public class getnotesfilefrompathconverter : ivalueconverter {     public object convert(object value, type targettype, object parameter, cultureinfo culture)    {        var txtfilepath = (string)value;        fileinfo txtfile = new fileinfo(txtfilepath);        if (txtfile.exists == false)            {                return string.format(@"file not found");            }        try        {            return file.readalltext(txtfilepath);        }             catch (exception ex){                return string.format("error: " + ex.tostring());            }    }      public object convertback(object value, type targettype, object parameter, cultureinfo culture)     {         return null;     }  

the converter applied in xaml:

        <textbox x:name="filepath_txt" >             <textbox.text>                   <![cdata[                 \\igtm.com\art\graphics\tst\820777\0010187775\69352c5d5c5d195.txt                 ]]>             </textbox.text>         </textbox>         <textbox x:name="filepathread_txt" text="{binding elementname=filepath_txt,path=text,converter={staticresource getnotesfilefrompathconverter},mode=oneway}" /> 

this working fine. however, if text in text file updated, not reflected in xaml. i've seen information on using filesystemwatcher, not sure how apply inside of converter text being returned updated. can help?

i wouldn't use converter in case since you'll need setup filesystemwatcher on file. bind text of filepath_txt property in view model , bind text of filepathread_txt property. update filesystemwatcher updates new file. if filename changes or file updated use logic had in converter update filepathread_txt property. if aren't familiar mvvm pattern, take @ msdn article.

in view model:

string filename; public string filename {    {return filename;}    set {       if (filename != value)       {          filename = value;          onnotifypropertychanged("filename");          watchfile();          updatefiletext();       } }  string filetext; public string filetext {    {return filetext;}    set {       filetext = value;       onnotifypropertychanged("filetext");    } }  private void watchfile() {    // create filesystemwatcher on filename    // call updatefiletext when file changed }  private void updatefiletext() {    // code converter    // set filetext } 

in xaml:

<textbox x:name="filepath_txt" text="{binding filename, updatesourcetrigger=propertychanged}"/> <textbox x:name="filepathread_txt" text="{binding filetext}" /> 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -