5、WPF 界面切换
WPF 界面切换的方式应当有三种,其一,设计多个窗口;其二,一个窗口使用控件切换;其三,一个窗口使用Page切换。
5.1 一个窗口使用控件切换
参考资料:
http://zhidao.baidu.com/link?url=bD-xnCCPNrX2eDdazN4LnjkynZAXffJyAHpfTlv40ARpaHMq8ZiglyAbl16KSvHz2lHXn6lSLjHIN1EroMPlsa
使用ContentControl控件,然后在button的click事件里动态创建ContentControl的内容(Content属性)
比如你的MainWindow窗口里有如下两个控件,一个ContentControl,一个button
<Grid>
<Button Content="Button" Width="100" Height="40" VerticalAlignment="Top" Margin="0,5,0,5" Click="button1_Click" />
<ContentControl Name="contentControl1" Margin="0,50,0,0" />
</Grid>
然后你在项目中新建一个UserControl名为UserControl1,随便添加一些内容,比如一个椭圆
<Grid>
<Ellipse Fill="Purple" />
</Grid>
最后,设置MainWindow中button的Click事件如下
private void button1_Click(object sender, RoutedEventArgs e)
{
contentControl1.Content = new UserControl1();
}
运行一下,点击button,那么ContentControl里面就切换到UserControl1的界面了!类似的,你可以定义很多自定义控件,设置不同的button分别切换到这些控件内容即可!
5.2 一个窗口使用Page切换
参考资料:
http://blog.sina.com.cn/s/blog_7a274a540101hdfd.html
private void tv12_Selected(object sender, System.Windows.RoutedEventArgs e)
{
this.frame.Source=new Uri("/Page2.xaml",UriKind.Relative);
}
其中tv12是标题项的名称,使用Treeview的Selected事件,frame是Frame控件的名称。上面的内容在“基于Expression Blend 4中文版 WPF和Silverlight项目设计基础”中有介绍。
原文:http://www.cnblogs.com/moiska/p/4904139.html