xaml - Transfer User Control to a WPF custom control -
xaml - Transfer User Control to a WPF custom control -
ok, have user control, want transfer wpf custom control.
the user command has code behind file, can straight register events of control: example:
<textbox grid.column="0" name="valuebox" textalignment="right" textchanged="valuebox_textchanged" previewkeydown="valuebox_previewkeydown" /> and corresponding valuebox_textchanged event in code behind is:
private void valuebox_textchanged(object sender, textchangedeventargs e) { var textbox = (textbox)sender; var positivewholenum = new regex(@"^-?\d+$"); if (!positivewholenum.ismatch(textbox.text) || int.parse(textbox.text) < 0) { textbox.text = "0"; } numericvalue = convert.toint32(textbox.text); raiseevent(new routedeventargs(valuechangedevent)); } now, when seek turn custom control, have theme folder, generic.xaml file. , file: mycustomcontrol.cs
in generic.xaml, i've written (i derive control)
<textbox grid.column="0" name="valuebox" textalignment="right" /> and in cs file:
public override void onapplytemplate() { base.onapplytemplate(); attachtextbox(); } protected textbox textbox; private void attachtextbox() { var textbox = gettemplatechild("valuebox") textbox; if (textbox != null) { textbox = textbox; } } now, how write replacement valuebox_textchanged written in user control?
you can write code valuebox_textchanged event handler in codebehind of generic.xaml file. create code behind fiel generic.xaml follow below steps.
add file named generic.xaml.cs
right click on themes folder , select new class name generic.xaml.cs change definition of class within generic.xaml.cs partial class generic :resourcedictionary //this class should inherit resourcedictionary link code behind file gereric.xaml using x:class
now can write code generic.xaml elements in generic.xaml.cs file.
for more info: code behind generic.xaml
wpf xaml control
Comments
Post a Comment