Change WPF Textbox Foreground Color on Event -
i change foreground color of textbox based on incoming event (the incoming number differs 1 in textbox) change black if text changed through ui. have working in circuitous way, i'm not sure of correct way it.
the xaml:
<textbox style="{staticresource recparm}" foreground="{binding path=acquisitiontimechangedbyinstrument, converter={staticresource booleantobrush}}" name="acquisitiontxtbox" textchanged="onacquisitiontimechanged" > <textbox.text> <binding path="acquisitiontime" mode="twoway" stringformat="{}{0:f6}" updatesourcetrigger="propertychanged" > <binding.validationrules> <vm:acquisitiontimerule min="200e-6" max="40" /> </binding.validationrules> </binding> </textbox.text> </textbox>
the code behind:
private void onacquisitiontimechanged(object sender, textchangedeventargs e) { //acquisitiontxtbox.foreground = brushes.black; ((viewmodel)application.current.resources["vm"]).acquisitiontimechangedbyinstrument = false; }
acquisitiontimechangedbyinstrument property raises propertychanged on viewmodel. converter change color black false , blue true.
- in form above, seems work described, seems odd way go it. if use commented line change color directly, binding seems break. is, view stops checking changes acquisitiontimechangedbyinstrument. why?
- what correct way this?
please keep in mind have been using wpf few days; don't understand advanced features yet.
edit (by request)
eventually check see if value in textbox has changed in acquisitiontime. now, setting acquisitiontimechangedbyinstrument=true when button clicked. send propertychanged event, called if haven't change acquisitiontxtbox.foreground in callback.
[valueconversion(typeof(bool), typeof(solidcolorbrush))] public class booleantobrushconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { if (null == value) { return null; } if (value bool) { if ((bool)value) { return (solidcolorbrush)brushes.deepskyblue; } return (solidcolorbrush)brushes.black; } type type = value.gettype(); throw new invalidoperationexception("no type " + type.name); } public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { throw new notimplementedexception(); } }
setting dependencyproperty locally can take precedence on other settings. effectively, end overwriting binding. use
acquisitiontxtbox.setcurrentvalue(foregroundproperty, brushes.black);
Comments
Post a Comment