c# - Inherited DataContext - bindings not working -
following situation: got base class provides little framework making modal dialogs adorner.
the base class has property of type datatemplate contains actual input scheme (all kinds of input possible) object property contains mapping model (a model class template binds it's input values).
because want reuse adorner, made have contentcontrol anon has contenttemplate actual dialog design. dialog's design contains contentcontrol template bound property in adorner class. datacontext of adorner's contentcontrol set itself, of course.
now embedded contentcontrol (in design) generates datatemplate , displays (in current case) textbox. textbox should bound model. therefore reused datacontext of adorner design template actual input template. here's how i've done it:
the adorner's controltemplate
<border grid.row="0" background="{dynamicresource inputadornerheaderbackground}" borderthickness="0,0,0,1" cornerradius="0"> <textblock horizontalalignment="stretch" verticalalignment="stretch" textwrapping="wrap" text="{binding header}" fontsize="{dynamicresource inputadornerheaderfontsize}" foreground="{dynamicresource inputadornerheaderforeground}" fontweight="{dynamicresource inputadornerheaderfontweight}" margin="8" /> </border> <border grid.row="1" borderthickness="1,0,1,1" borderbrush="{dynamicresource inputadornerborderbrush}" cornerradius="0"> <contentcontrol contenttemplate="{binding inputcontroltemplate}" horizontalalignment="stretch" verticalalignment="stretch" /> </border> </grid> </contenttemplate>
the actual input template (datatemplate)
<datatemplate x:key="textinputtemplate"> <grid background="black" datacontext="{binding datacontext.inputmapping, relativesource={relativesource ancestortype={x:type contentcontrol}}}"> <textbox text="{binding path=text, mode=onewaytosource}" horizontalalignment="stretch" verticalalignment="stretch" borderthickness="0" adorners:inputadorner.focuselement="{binding relativesource={relativesource self}}" /> </grid> </datatemplate>
model class
public sealed class textinputmodel { public string text { get; set; } }
adorner properties
public datatemplate inputcontroltemplate { { return _inputcontroltemplate; } private set { if (equals(value, _inputcontroltemplate)) return; _inputcontroltemplate = value; onpropertychanged(); } } public object inputmapping { { return _inputmapping; } private set { if (equals(value, _inputmapping)) return; _inputmapping = value; onpropertychanged(); } }
fyi: model being dynamically instantiated when adorner being created. not set twice. must kind of binding issue.
the template shows correctly. see , can input stuff textbox, once fetch model properties default (""). did work 1 or 2 times somehow design changes have made disfunctional.
i don't interfering here point of view should set correctly. checked context of datatemplate: actual model class. yet textbox inputs not update property.
edit: reason seems attatched property causing issue. why interfering? not override datacontext, it?
Comments
Post a Comment