suppose have 2 different windows in wpf application window1 , window2.
based on actions window1, window2 pop while, , based on actions in window1, window2 should close , view returns window1 it's state before window2 appears.
with it's state mean content in controls exists in window1.
to achieve switching used
showdialog();
which looking because need window1 freeze while window2 on.
right problem can't return window1 it's content.
mainwindow (window1).xaml
<window x:class="test.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <grid> <button content="button" horizontalalignment="left" verticalalignment="top" width="75" margin="193,130,0,0" click="button_click"/> </grid></window>
button_click
private void button_click(object sender, routedeventargs e) { window1 window1 = new window1(); window1.showdialog(); //actions window1.close(); }
and window1 xaml normal 1 didn't change except made
windowstyle=none
so can't exit right top exit button
if using mvvm can use this:
public static class dialogcloser { public static readonly dependencyproperty dialogresultproperty = dependencyproperty.registerattached( "dialogresult", typeof(bool?), typeof(dialogcloser), new propertymetadata(dialogresultchanged)); private static void dialogresultchanged( dependencyobject d, dependencypropertychangedeventargs e) { var window = d window; if (window != null) window.dialogresult = e.newvalue bool?; } public static void setdialogresult(window target, bool? value) { target.setvalue(dialogresultproperty, value); } }
and in xaml:
<window x:class="window2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/ xmlns:xc="clr-namespace:window2" xc:dialogcloser.dialogresult="{binding dialogresult}"
then in model can use:
public bool? dialogresult { get; set; }
Comments
Post a Comment