c# - How to raise a textbox TextChangedEvent programmatically -
i'm using .net 4.5, in wpf project. raise textbox textchangedevent. have done so:
tb.raiseevent(new routedeventargs(textbox.textchangedevent)); i've done same sort of raiseevent on button.clickevent before, how can one?
this produces error follows:
exception:thrown: "object of type 'system.windows.routedeventargs' cannot converted type 'system.windows.controls.textchangedeventargs'." (system.argumentexception) system.argumentexception thrown: "object of type 'system.windows.routedeventargs' cannot converted type 'system.windows.controls.textchangedeventargs'."
[edit]
the actual textbox text changed handled attached behavior follows. place want programmatically raise event in control's attached behavior. in later behavior have textbox object.
public class textboxtextchangedbehavior : behavior<textbox> { protected override void onattached() { base.onattached(); associatedobject.textchanged += ontextchanged; } protected override void ondetaching() { associatedobject.textchanged -= ontextchanged; base.ondetaching(); } private void ontextchanged(object sender, textchangedeventargs args) { var textbox = (sender textbox); if (textbox != null) { //populate observablecollection } } } place seek raise event:
public class findpopupbehavior : behavior<popup> { protected override void onattached() { associatedobject.opened += _openfindpopup; } protected override void ondetaching() { associatedobject.opened -= _openfindpopup; } void _openfindpopup(object sender, eventargs e) { //fake textbox text changed event if (textboxobject == null) return; textbox tb = textboxobject textbox; if (tb.text == "") return; tb.raiseevent(new routedeventargs(textbox.textchangedevent)); } public static readonly dependencyproperty textboxproperty = dependencyproperty.register("textboxobject", typeof(textbox), typeof(findpopupbehavior), new uipropertymetadata(null)); public object textboxobject { { homecoming (object)getvalue(textboxproperty); } set { setvalue(textboxproperty, value); } } } [edit2]
the textbox resides in popup upon popup closing observablecollection cleared, textbox text remains, although hidden. if popup reopened , textbox has text needs repopulate observablecollection. population in textchanged behavior. why thinking false event.
manually raising .net events unadvisable, if no other reason unrequired. think it... don't really want raise event. you're not seeking functionality of event, why raise it?
if understand situation correctly, surely, actually want execute particular section of code. such, problem caused because section of code encapsulated within event handler. however, doesn't mean have raise event execute code.
instead, move relevant code method, can phone call event handler and wherever else want , without unnecessarily raising events:
private void ontextchanged(object sender, textchangedeventargs args) { var textbox = (sender textbox); if (textbox != null) { populateobservablecollection(); } } private void populateobservablecollection() { // populate observablecollection } if particularly need access textbox, please describe situation further... there always way.
update >>>
think logically... want phone call method , need access textbox in popup , need each time popup opened. why not handle popup.opened event?:
private void popupopened(object sender, eventargs e) { // check textbox , populate observablecollection } c# wpf textbox raiseevent
No comments:
Post a Comment