c# - Override event in UIElement initialization statement -
i created secondarywindow dynamically has windowstyle
set none
. hence, want set content dragmove-able overriding onmouseleftbuttondown
.
i not figure out how include override function within secondarywindow initialization statement
public class mainwindow { window secondarywindow = new window { windowstyle = system.windows.windowstyle.none, content = new myusercontrol(), topmost = true, // failed attempt base.onmouseleftbuttondown += (object sender, mousebuttoneventargs e) => { base.onmouseleftbuttondown(e); base.dragmove(); } }; }
your question more asked 'how add handler event in object initializer?'. 'object initializer' refers syntax this:
foo newfow = new foo { foo.property = somevalue };
just make sure don't misunderstand something, onmouseleftbuttondown += smth
not override event, adds event handler event.
that being said: can't. c# doesn't support registering handlers on events in object initializer:
// not possible foo newfoo = new foo { event += somehandler };
neither c# let set event:
// not possible foo newfoo = new foo { event = somedelegate };
however, can work around limitation, wrapping event of class secondarywindow in property:
public class secondarywindow : window { public mousebuttoneventhandler mouseleftbuttondownsubscriber { set { mouseleftbuttondown += value; } } }
no can initialize object this:
window secondarywindow = new window { windowstyle = system.windows.windowstyle.none, content = new myusercontrol(), topmost = true, mouseleftbuttondownsubscriber = (object sender, mousebuttoneventargs e) => { base.onmouseleftbuttondown(e); base.dragmove(); } };
i not recommend though, adds confusion people not familiar code , there no reason this, apart convenience of using object initializer. recommend initialize object , set properties in initializers, subscribe events 1 expect it, example in constructor of parent window.
Comments
Post a Comment