c# - Attach event handler from parent class in xaml -



c# - Attach event handler from parent class in xaml -

i have base of operations class window contains number of event handlers mutual windows of type (they fire mutual validation methods).

here illustration event handler:

protected virtual void validatetextboxtextchanged(object sender, system.windows.controls.textchangedeventargs e) { validateproperty((frameworkelement)sender, textbox.textproperty); }

and there approximately 20 of these, covering mutual controls.

each window instance built kid class of base. in .xaml of kid class do:

<textbox textchanged="validatetextboxtextchanged"/>

but when navigate window, next error:

failed assign property 'system.windows.controls.textbox.textchanged'.

so it's not finding event handler. there elegant way can assign event handlers without duplicating them in every kid class (~ 30 children , counting)?

that's interesting -- seems limitation of event mechanism check partial code-behind class, , not base of operations class. (this issue has been raised here , here.) can't think of ideal work-around, either.

of course, prepare error putting dummy override in every kid class:

protected override void validatetextboxtextchanged(object sender, textchangedeventargs e) { base.validatetextboxtextchanged(sender, e); }

now, granted seems hassle. alternative utilize trigger/action combination phone call base-class method:

<textbox> <i:interaction.triggers> <i:eventtrigger eventname="textchanged"> <ei:callmethodaction targetobject="{binding relativesource={relativesource ancestortype=local:basewindow}}" methodname="validatetextboxtextchanged" /> </i:eventtrigger> </i:interaction.triggers> </textbox>

for this, need create method public. utilize variations on above simplify syntax , cut down number of bindings: utilize attached property apply trigger, set validation method in static resources optional override.

another possible approach subclass textbox itself, , place validation logic there. expose dependency property allow instances of extended textbox override default validation logic.

c# xaml silverlight events inheritance

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -