c# - Inject controls in derived windows breaks xaml markup in combination with binding -


i trying inject layoutgrid , canvas windows, causes little headache:

here windowbase class:

public class windowbase : window {     protected override void oninitialized(eventargs e)     {         base.oninitialized(e);          if (content frameworkelement)         {             var originalcontent = content frameworkelement;             var grid = new grid();             grid.datacontext = originalcontent.datacontext;             content = grid;             grid.children.add(originalcontent);              var canvas = new canvas() { horizontalalignment = horizontalalignment.stretch, verticalalignment = verticalalignment.stretch };             grid.children.add(canvas);         }     } } 

my mainwindow thats inheriting windowbase looks this:

xaml:

<local:windowbase x:class="insertcanvastest.mainwindow"                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"                   xmlns:local="clr-namespace:insertcanvastest"                   datacontext="{binding relativesource={relativesource self}}">     <border>         <grid>             <combobox selectedindex="1"                       itemssource="{binding itemsource1}" />         </grid>     </border> </local:windowbase> 

code behind of mainwindow:

public partial class mainwindow : windowbase {     private list<int> _itemsource1;     public list<int> itemsource1     {                 {             if (_itemsource1 == null)                 _itemsource1 = new list<int>(){1,2,3};             return _itemsource1;         }      }      public mainwindow()     {         initializecomponent();     } } 

as can see in xaml have specified selectedindex should 1, code in windowbase trying inject canvas , grid information gets lost , selectedindex @ -1.

is there way fix this?

i keep mainwindow window , not implement control , load different window inside contentpresenter of so.

i know problem wouldnt exist if declared canvas/grid in xaml directly instead of trying inject in codebehind, doing 100+ windows , trying maintain them if changes annoying.

change windowbase class :

windowbase

[contentproperty("internalcontent")] public class windowbase : window {     // internalcontent     public static readonly dependencyproperty internalcontentproperty =          dependencyproperty.register( "internalcontent", typeof(object),         typeof(windowbase), new frameworkpropertymetadata(null));      public object internalcontent     {         { return getvalue(internalcontentproperty); }         set { setvalue(internalcontentproperty, value); }     }      ... }  <window ...>     <grid>         ...         <contentcontrol istabstop="false"             content="{binding relativesource={relativesource mode=findancestor, ancestortype=window}, path=internalcontent}" />         <canvas />         ...     </grid> </window> 

in essence, create new internalcontent property subclasses see default content (thanks contentpropertyattribute), , display content contentcontrol.

that said, there better ways you're trying inheritance. using templates comes mind. or maybe adorners if want "above layer display things" canvas suggest.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -