标签:
也是由于没用足够时间在查找相关书籍知识上,导致项目进程一直拖延不前,惭愧惭愧;
关于之前的展示制作在这里做一个修正:
1.以前的通过进程将两个window联系起来着实麻烦而且是两个项目,无法很好的共用,所以修改为添加新窗口,操作界面如下:
在红色圈部分右键--添加--窗口即可得到一个与MainWindow一个级别的window窗口;
窗口转换可通过.Show()事件实现;
接下来就是保存用户操作数据到xml以及查看数据通过xml;
我用的是保存和修改xml文件的形式;
xml文件如何与xaml之间数据交换绑定我的博客随笔:WPF中后台存储数据方法一XML有一定的解释;
在这里第二步也具体的解释一下:
1 <Window x:Class="CuirriculumBegin.editwin" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="editwin" Height="500" Width="400"> 5 <Grid> 6 <TextBlock HorizontalAlignment="Left" Margin="14,113,0,0" TextWrapping="Wrap" Text="New Class Name:" VerticalAlignment="Top" Height="30" Width="139" RenderTransformOrigin="0.537,0.527" FontFamily="Malgun Gothic" FontSize="16" FontWeight="Bold"/> 7 <TextBox x:Name="course_name_box" HorizontalAlignment="Left" Height="23" Margin="153,113,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="213" FontSize="16" FontFamily="News706 BT"/> 8 <TextBlock HorizontalAlignment="Left" Margin="14,282,0,0" TextWrapping="Wrap" Text="Class Time:" VerticalAlignment="Top" Height="30" Width="92" RenderTransformOrigin="0.543,0.696" FontFamily="Malgun Gothic" FontSize="16" FontWeight="Bold"/> 9 10 <TextBlock HorizontalAlignment="Left" Margin="83,169,0,0" TextWrapping="Wrap" Text="Teacher:" VerticalAlignment="Top" Height="30" Width="70" RenderTransformOrigin="0.537,0.527" FontFamily="Malgun Gothic" FontSize="16" FontWeight="Bold"/> 11 <TextBox x:Name="teacher_name_box" HorizontalAlignment="Left" Height="23" Margin="153,169,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="213" FontSize="16" FontFamily="News706 BT" /> 12 <TextBlock HorizontalAlignment="Left" Margin="101,225,0,0" TextWrapping="Wrap" Text="Place:" VerticalAlignment="Top" Height="30" Width="52" RenderTransformOrigin="0.537,0.527" FontFamily="Malgun Gothic" FontSize="16" FontWeight="Bold"/> 13 <TextBox x:Name="place_box" HorizontalAlignment="Left" Height="23" Margin="153,225,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="213" FontSize="16" FontFamily="News706 BT"/> 14 <Button x:Name="save" Content="Save" HorizontalAlignment="Left" Margin="46,387,0,0" VerticalAlignment="Top" Width="75" Click="save_Click"/> 15 <Button Content="Exit" HorizontalAlignment="Left" Margin="291,387,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/> 16 <ComboBox x:Name="combobox1" HorizontalAlignment="Left" Margin="123,282,0,0" VerticalAlignment="Top" Width="90"> 17 <ComboBoxItem> 18 <TextBlock x:Name="one">星期一</TextBlock> 19 </ComboBoxItem> 20 <ComboBoxItem> 21 <TextBlock x:Name="two">星期二</TextBlock> 22 </ComboBoxItem> 23 <ComboBoxItem> 24 <TextBlock x:Name="three">星期三</TextBlock> 25 </ComboBoxItem> 26 <ComboBoxItem> 27 <TextBlock x:Name="four">星期四</TextBlock> 28 </ComboBoxItem> 29 <ComboBoxItem> 30 <TextBlock x:Name="five">星期五</TextBlock> 31 </ComboBoxItem> 32 <ComboBoxItem> 33 <TextBlock x:Name="six">星期六</TextBlock> 34 </ComboBoxItem> 35 <ComboBoxItem> 36 <TextBlock x:Name="seven">星期日</TextBlock> 37 </ComboBoxItem> 38 </ComboBox> 39 <ComboBox x:Name="combobox2" HorizontalAlignment="Left" Margin="241,282,0,0" VerticalAlignment="Top" Width="125"> 40 <ComboBoxItem> 41 <TextBlock x:Name="time1">上午第一节课</TextBlock> 42 </ComboBoxItem> 43 <ComboBoxItem> 44 <TextBlock x:Name="time2">上午第二节课</TextBlock> 45 </ComboBoxItem> 46 <ComboBoxItem x:Name="time3"> 47 <TextBlock>上午第三节课</TextBlock> 48 </ComboBoxItem> 49 <ComboBoxItem x:Name="time4"> 50 <TextBlock>上午第四节课</TextBlock> 51 </ComboBoxItem> 52 <ComboBoxItem x:Name="time5"> 53 <TextBlock>下午第一节课</TextBlock> 54 </ComboBoxItem> 55 <ComboBoxItem> 56 <TextBlock x:Name="time6">下午第二节课</TextBlock> 57 </ComboBoxItem> 58 <ComboBoxItem> 59 <TextBlock x:Name="time7">下午第三节课</TextBlock> 60 </ComboBoxItem> 61 <ComboBoxItem> 62 <TextBlock x:Name="time8">下午第四节课</TextBlock> 63 </ComboBoxItem> 64 <ComboBoxItem> 65 <TextBlock x:Name="time9">晚上</TextBlock> 66 </ComboBoxItem> 67 </ComboBox> 68 <Button Content="DeleteALL" HorizontalAlignment="Left" Margin="165,387,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/> 69 </Grid> 70 </Window>
这是一个简单的对话框界面;
1 public void CreateXMLDocument() 2 { 3 XmlDocument xmlDoc = new XmlDocument(); 4 5 //加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?> 6 XmlDeclaration xmlDeclar; 7 xmlDeclar = xmlDoc.CreateXmlDeclaration("1.0", "gb2312", null); 8 xmlDoc.AppendChild(xmlDeclar); 9 10 XmlElement xmlElement = xmlDoc.CreateElement("", "Courses", ""); 11 xmlDoc.AppendChild(xmlElement); 12 13 //添加节点 14 XmlNode root = xmlDoc.SelectSingleNode("Courses"); 15 xmlDoc.Save("../../Courses.xml");//保存的路径 16 }
添加节点到已有xml:
1 public void addtoxml(string teacher_name, string course_name, string place, string timerow, string timecol) 2 { 3 XmlDocument xmlDoc = new XmlDocument(); 4 xmlDoc.Load("../../Courses.xml");//找到xml文件 5 XmlNode root = xmlDoc.SelectSingleNode("Courses"); 6 XmlElement xe1 = xmlDoc.CreateElement("course"); 7 xe1.SetAttribute("teacher_name", teacher_name); 8 xe1.SetAttribute("course_name", course_name); 9 xe1.SetAttribute("place", place); 10 xe1.SetAttribute("timerow", timerow); 11 xe1.SetAttribute("timecol", timecol); 12 root.AppendChild(xe1); 13 xmlDoc.Save("../../Courses.xml"); 14 }
删除xml节点:
1 public void DeleteNode() 2 { 3 XmlDocument xmlDocument = new XmlDocument(); 4 xmlDocument.Load("../../Courses.xml"); 5 6 XmlNodeList xnl = xmlDocument.SelectSingleNode("Courses").ChildNodes; 7 8 foreach (XmlNode xn in xnl) 9 { 10 if (xn.Name == "course") 11 { 12 XmlElement xe = (XmlElement)xn;//将xn的属性转换为XmlElement 13 xe.RemoveAttribute("teacher_name"); 14 xe.RemoveAttribute("course_name"); 15 xe.RemoveAttribute("place"); 16 xe.RemoveAttribute("timerow"); 17 xe.RemoveAttribute("timecol"); 18 } 19 } 20 xmlDocument.Save("../../Courses.xml"); 21 }
具体的操作解释见博客:http://www.jb51.net/article/56289.htm
在我的save按钮中的点击事件中添加如下代码即可:
1 private void save_Click(object sender, RoutedEventArgs e) 2 { 3 CreateXMLDocument(); 4 string timerow = "0", timecol = "0"; 5 switch (combobox1.Text) 6 { 7 case "星期一": timecol = "1"; break; 8 case "星期二": timecol = "2"; break; 9 case "星期三": timecol = "3"; break; 10 case "星期四": timecol = "4"; break; 11 case "星期五": timecol = "5"; break; 12 case "星期六": timecol = "6"; break; 13 case "星期日": timecol = "7"; break; 14 default: MessageBox.Show("非法输入!"); break; 15 } 16 switch (combobox2.Text) 17 { 18 case "上午第一节课": timerow = "1"; break; 19 case "上午第二节课": timerow = "2"; break; 20 case "上午第三节课": timerow = "3"; break; 21 case "上午第四节课": timerow = "4"; break; 22 case "下午第一节课": timerow = "5"; break; 23 case "下午第二节课": timerow = "6"; break; 24 case "下午第三节课": timerow = "7"; break; 25 case "下午第四节课": timerow = "8"; break; 26 case "晚上": timerow = "9"; break; 27 default: MessageBox.Show("非法输入!"); break; 28 } 29 addtoxml(teacher_name_box.Text, course_name_box.Text, place_box.Text, timerow, timecol); 30 }
我创建了三个窗口:
一个是MainWindow就是我的母体窗口也就是我在课程表WPF制作 学习步骤二中的窗口;
一个是editwin窗口就是上面的窗口;
一个是Window1窗口,就是中的课程表WPF制作 学习步骤一主界面
二、XAML与XML的数据绑定
具体的XML与XAML数据绑定是在我的Window1窗口中实现的,也就是说主界面需要与数据进行绑定,代码如下:
1 <Window x:Class="CuirriculumBegin.window1" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="window1" WindowState="Maximized"> 5 <Window.Resources> 6 <XmlDataProvider x:Key="course-info" Source="Courses.xml" XPath="/Courses/*"></XmlDataProvider> 7 </Window.Resources> 8 <Grid> 9 <Grid DataContext="{Binding Source={StaticResource course-info}}"> 10 <Grid.RowDefinitions > 11 <RowDefinition Height="90"></RowDefinition> 12 <RowDefinition></RowDefinition> 13 <RowDefinition></RowDefinition> 14 <RowDefinition></RowDefinition> 15 <RowDefinition></RowDefinition> 16 <RowDefinition></RowDefinition> 17 <RowDefinition Height="40"></RowDefinition> 18 </Grid.RowDefinitions> 19 <Grid.ColumnDefinitions> 20 <ColumnDefinition Width="100"></ColumnDefinition> 21 <ColumnDefinition></ColumnDefinition> 22 <ColumnDefinition></ColumnDefinition> 23 <ColumnDefinition></ColumnDefinition> 24 <ColumnDefinition></ColumnDefinition> 25 <ColumnDefinition></ColumnDefinition> 26 <ColumnDefinition></ColumnDefinition> 27 <ColumnDefinition></ColumnDefinition> 28 </Grid.ColumnDefinitions> 29 <TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" Foreground="#FFBD1096">星期一</TextBlock> 30 <TextBlock Grid.Row="0" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" Foreground="#FFBD1096">星期二</TextBlock> 31 <TextBlock Grid.Row="0" Grid.Column="3" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" Foreground="#FFBD1096">星期三</TextBlock> 32 <TextBlock Grid.Row="0" Grid.Column="4" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" Foreground="#FFBD1096">星期四</TextBlock> 33 <TextBlock Grid.Row="0" Grid.Column="5" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" Foreground="#FFBD1096">星期五</TextBlock> 34 <TextBlock Grid.Row="0" Grid.Column="6" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" Foreground="#FFBD1096">星期六</TextBlock> 35 <TextBlock Grid.Row="0" Grid.Column="7" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" Foreground="#FFBD1096">星期日</TextBlock> 36 <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20">时间段</TextBlock> 37 <TextBlock Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20" Text=" 8:00
 -- 
 9:35"></TextBlock> 38 <TextBlock Grid.Row="2" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20" Text=" 9:55
 -- 
11:30"></TextBlock> 39 <TextBlock Grid.Row="3" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20" Text="14:00
 -- 
15:35"></TextBlock> 40 <TextBlock Grid.Row="4" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20" Text="15:55
 -- 
17:30"></TextBlock> 41 <TextBlock Grid.Row="5" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" Text="晚上"></TextBlock> 42 <Image Source="Resources\1.jpeg" Grid.ColumnSpan="8" Grid.RowSpan="7" Opacity="0.4" Stretch="Fill"/> 43 <Button x:Name="edit_btn" Grid.Column="2" Grid.Row="6" Margin="0,0,5,10" HorizontalAlignment="Right" Width="110" RenderTransformOrigin="0.511,0.067" Content="Edit" IsCancel="True" BorderBrush="#FF1C84EC" Background="White" Opacity="0.5" Foreground="#FFF50808" OpacityMask="#FFF7F7F7" FontFamily="Simplified Arabic" FontSize="16" Grid.ColumnSpan="2" Click="edit_btn_Click"/> 44 <Button x:Name="save_btn" Grid.Column="4" Grid.Row="6" Margin="0,0,79,10" HorizontalAlignment="Right" Width="110" RenderTransformOrigin="0.511,0.067" Content="Save" IsCancel="True" BorderBrush="#FF1C84EC" Background="White" Opacity="0.5" Foreground="#FFF50808" OpacityMask="#FFF7F7F7" FontFamily="Simplified Arabic" FontSize="16" Grid.ColumnSpan="2" Click="save_btn_Click"/> 45 <StackPanel Grid.Column="{Binding XPath=@timecol}" Grid.Row="{Binding XPath=@timerow}"> 46 <TextBlock Text="{Binding XPath=@place}"></TextBlock> 47 <TextBlock Text="{Binding XPath=@course_name}"></TextBlock> 48 <TextBlock Text="{Binding XPath=@teacher_name}"></TextBlock> 49 </StackPanel> 50 </Grid> 51 </Grid> 52 </Window>
关于XML与XAML交换的具体解释可详细见参考博客:http://blog.sina.com.cn/s/blog_3d6eb75d0100mqtl.html
这样大体的成型框架就实现了,操作:
点击Save按钮在Window1中显示如下:
那么问题来了:为什么在设计器里面xaml显示出来了数据而在运行后显示却没有???
运行后如下:
还是要继续学习。。。。。。。
Over~~
标签:
原文地址:http://www.cnblogs.com/Elson8080/p/4491296.html