Wednesday, 15 January 2014

c# - WPF Custom Controls Constuction,Triggers and Events -



c# - WPF Custom Controls Constuction,Triggers and Events -

wpf custom controls

i want build new custom control. found few tutorials gave me clue of how accomplish this. understand it, creating new custom command done extending current one, , possible extend command basic levels of hierarchy, in example, can extend:

uielement frameworkelement control contentcontrol headeredcontentcontrol itemscontrol selector rangebase

as written in next tutorial: http://wpftutorial.net/howtocreateacustomcontrol.html

so, followed tutorial , created new project of type custom command library, got generic .xaml , code behind. far good.

there 3 types or categories of events can distinguish between.

events consumable window (or container) utilize control: events expose outside. how write those?

events related command , not beingness exposed outside; example, if mouse on command , react that. can in xaml:

<controltemplate.triggers> <trigger property="ismouseover" value="true"> <setter property="fill" targetname="lefttraingularindicator"> <setter.value> <solidcolorbrush color="yellow" /> </setter.value> </setter> </trigger> </controltemplate.triggers>

assuming of couse have element fill property in controltemplate named x:name="lefttraingularindicator"

questions:

now want react in xaml ismousedown. how do that? there no "ismousedown" trigger. furthermore if want react in code behind? , farther if want alter lefttraingularindicator "fill" code behind?

events related sub-elements part of visual construction of controltemplate, in illustration if want react "ismouseover" of "lefttraigularindicator" in xaml / or in code behind? maybe both.

i attempting 2 days now... feeling missing in understanding of how things work. didn't find tutorial explains questions.

i see few lines of illustration each of questions surfaced here.

thanks.

after extensive research...

1) exposing events outside... if exposing other class.

public delegate void mydelegate(int somevalue); public event mydelegate myevent;

and somewhere in code:

if(myevent!=null) myevent(5);

nothing new in part.

2) code behind create instance constructor

public mycustomcontrol() { mousemove += mycustomcontrol_mousemove; } void mycustomcontrol_mousemove(object sender, mouseeventargs e) { //now can react motion of mouse. //if illustration want address element, let's rectangle: var ele = (rectangle)template.findname("myrect",this); ele.fill=mynewbrush; // provided have element named "myrect" (x:name="myrect") in // generic.xaml style->control template-> corresponds name. }

3) not recommended - belong scope of user-controls , not custom controls. custom controls "atoms", user controls more suitable purpose of combining controls.

but not impossible...

var mybutton = (button)template.findname("mybutton",this); mybutton.onmousemove += ....

just maintain in mind that:

anything should known in code-behind must named.

your xaml should have no knowledge of code behind do. (except! - maintain reading)

parts should known in code-behind must have right name when design xaml.

i hope help others "wall" when trying develop own custom-controls.

c# .net wpf wpf-controls custom-controls

No comments:

Post a Comment