c# - Custom WPF MessageBox text is empty -
i have custom wpf messagebox
, upon specific message caption appearing in random location of parent window. custom messagebox
looks this:
public static class wpfmessagebox { public static messageboxresult show(string messageboxtext) { return showcore(null, messageboxtext); } public static messageboxresult show(string messageboxtext, string caption) { return showcore(null, messageboxtext, caption); } public static messageboxresult show(window owner, string messageboxtext) { return showcore(owner, messageboxtext); } public static messageboxresult show(string messageboxtext, string caption, messageboxbutton button) { return showcore(null, messageboxtext, caption, button); } public static messageboxresult show(window owner, string messageboxtext, string caption) { return showcore(owner, messageboxtext, caption); } public static messageboxresult show(string messageboxtext, string caption, messageboxbutton button, messageboximage icon) { return showcore(null, messageboxtext, caption, button, icon); } public static messageboxresult show(window owner, string messageboxtext, string caption, messageboxbutton button) { return showcore(owner, messageboxtext, caption, button); } public static messageboxresult show(string messageboxtext, string caption, messageboxbutton button, messageboximage icon, messageboxresult defaultresult) { return showcore(null, messageboxtext, caption, button, icon, defaultresult); } public static messageboxresult show(window owner, string messageboxtext, string caption, messageboxbutton button, messageboximage icon) { return showcore(owner, messageboxtext, caption, button, icon); } public static messageboxresult show(string messageboxtext, string caption, messageboxbutton button, messageboximage icon, messageboxresult defaultresult, messageboxoptions options) { return showcore(null, messageboxtext, caption, button, icon, defaultresult, options); } public static messageboxresult show(window owner, string messageboxtext, string caption, messageboxbutton button, messageboximage icon, messageboxresult defaultresult) { return showcore(owner, messageboxtext, caption, button, icon, defaultresult); } public static messageboxresult show(window owner, string messageboxtext, string caption, messageboxbutton button, messageboximage icon, messageboxresult defaultresult, messageboxoptions options) { return showcore(owner, messageboxtext, caption, button, icon, defaultresult, options); } private static messageboxresult showcore( window owner, string messageboxtext, string caption = "", messageboxbutton button = messageboxbutton.ok, messageboximage icon = messageboximage.none, messageboxresult defaultresult = messageboxresult.none, messageboxoptions options = messageboxoptions.none) { if (caption == "specific message caption") { var maxheight = owner.actualheight - 300; var maxwidth = owner.actualwidth - 500; var randomheight = getrandomnumber(2, maxheight); var randomwidth = getrandomnumber(2, maxwidth); return wpfmessageboxwindow.show( delegate(window messageboxwindow) { messageboxwindow.windowstartuplocation = windowstartuplocation.manual; messageboxwindow.left = randomwidth; messageboxwindow.top = randomheight; messageboxwindow.owner = owner; }, messageboxtext, caption, button, icon, defaultresult, options); } return wpfmessageboxwindow.show( delegate(window messageboxwindow) { messageboxwindow.owner = owner; }, messageboxtext, caption, button, icon, defaultresult, options); } private static double getrandomnumber(double minimum, double maximum) { random random = new random(); return random.nextdouble() * (maximum - minimum) + minimum; } }
the wpf messagebox window looks :
public partial class wpfmessageboxwindow : window { public wpfmessageboxwindow() { initializecomponent(); } private messageboxviewmodel _viewmodel; public static messageboxresult show( action<window> setowner, string messageboxtext, string caption, messageboxbutton button, messageboximage icon, messageboxresult defaultresult, messageboxoptions options) { if ((options & messageboxoptions.defaultdesktoponly) == messageboxoptions.defaultdesktoponly) { throw new notimplementedexception(); } if ((options & messageboxoptions.servicenotification) == messageboxoptions.servicenotification) { throw new notimplementedexception(); } _messageboxwindow = new wpfmessageboxwindow() { showintaskbar = false, topmost = true, resizemode = resizemode.noresize }; setowner(_messageboxwindow); playmessagebeep(icon); _messageboxwindow._viewmodel = new messageboxviewmodel(_messageboxwindow, caption, messageboxtext, button, icon, defaultresult, options); _messageboxwindow.datacontext = _messageboxwindow._viewmodel; _messageboxwindow.showdialog(); return _messageboxwindow._viewmodel.result; } private static void playmessagebeep(messageboximage icon) { switch (icon) { //case messageboximage.hand: //case messageboximage.stop: case messageboximage.error: systemsounds.hand.play(); break; //case messageboximage.exclamation: case messageboximage.warning: systemsounds.exclamation.play(); break; case messageboximage.question: systemsounds.question.play(); break; //case messageboximage.asterisk: case messageboximage.information: systemsounds.asterisk.play(); break; default: systemsounds.beep.play(); break; } } [threadstatic] private static wpfmessageboxwindow _messageboxwindow; protected override void onsourceinitialized(eventargs e) { // removes application icon window top left corner // different hiding windowhelper.removeicon(this); switch (_viewmodel.options) { case messageboxoptions.none: break; case messageboxoptions.rightalign: windowhelper.setrightaligned(this); break; case messageboxoptions.rtlreading: break; case messageboxoptions.rightalign | messageboxoptions.rtlreading: break; } // disable close button if needed , remove resize menu items window system menu var systemmenuhelper = new systemmenuhelper(this); if (_viewmodel.buttonoption == messageboxbutton.yesno) { systemmenuhelper.disableclosebutton = true; } systemmenuhelper.removeresizemenu = true; } private void window_keydown(object sender, keyeventargs e) { if (e.key == key.escape) { _viewmodel.escapecommand.execute(null); } } protected override void onclosed(eventargs e) { base.onclosed(e); _viewmodel.closecommand.execute(null); } }
this how use messagebox specific caption in code :
dynamic parentwindow = window.getwindow(this); var result = wpfmessagebox.show(parentwindow, "message text", "specific message cation", messageboxbutton.yesno, messageboximage.question);
my problem once in while, specific message, messagebox
text appearing empty:
here xaml :
<window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:messageboxutils" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d" x:class="messageboxutils.wpfmessageboxwindow" title="{binding title}" resizemode="noresize" flowdirection="{binding titleflowdirection}" showintaskbar="false" windowstartuplocation="centerscreen" sizetocontent="height" keydown="window_keydown" width="500" d:designwidth="500" height="300"> <grid> <grid flowdirection="{binding contentflowdirection}"> <grid.rowdefinitions> <rowdefinition height="60" /> <rowdefinition height="2" /> <rowdefinition height="*" /> <rowdefinition height="90" /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="60" /> <columndefinition width="*" /> </grid.columndefinitions> <image source="{binding messageimagesource}" horizontalalignment="left" verticalalignment="center" height="32" width="32" margin="10 0 0 0" /> <textblock fontsize="28" text="{binding message}" grid.rowspan="3" grid.column="1" textwrapping="wrap" textalignment="left" horizontalalignment="{binding contenttextalignment}" verticalalignment="top" margin="10,10,10,0" /> <border grid.row="3" grid.column="0" grid.columnspan="2" background="#ffe6e6e6" borderthickness="0 1 0 0" borderbrush="#ffdfdfdf"> <stackpanel orientation="horizontal" verticalalignment="center" horizontalalignment="right" margin="0,18,10,10" height="63" > <button fontsize="25" content="_yes" visibility="{binding yesnovisibility}" command="{binding yescommand}" isdefault="{binding isyesdefault}" margin="5 5 5 5" height="42" width="80" /> <button fontsize="25" content="_no" visibility="{binding yesnovisibility}" command="{binding nocommand}" isdefault="{binding isnodefault}" margin="5 5 5 5" height="42" width="80" /> <button fontsize="25" content="o_k" visibility="{binding okvisibility}" command="{binding okcommand}" isdefault="{binding isokdefault}" margin="5 5 5 5" height="42" width="80" /> <button fontsize="25" content="_cancel" visibility="{binding cancelvisibility}" command="{binding cancelcommand}" isdefault="{binding iscanceldefault}" margin="5 5 5 5" height="42" width="80" /> </stackpanel> </border> </grid> </grid>
is knows why?
you're binding text property named 'message' don't see in vm ... unless i've missed it!
anyway bet you're not issuing notifypropertychanged event when property updated, if viewmodel created before view view queries vm , well, if vm populated little after view view received empty string when queried. property updated in vm, view never gets told it. hunch.
Comments
Post a Comment