WPF系列

时间:2023-03-09 16:52:30
WPF系列

一、ListView绑定数据源XML

 //前端代码
1 <Window x:Class="ListView读取XML数据.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">
<Window.Resources>
<XmlDataProvider x:Key="Student" Source="Book.xml">
</XmlDataProvider>

</Window.Resources>
<Grid>
<ListView ItemsSource="{Binding Source={StaticResource Student},XPath=/Person/Information}">
</ListView>
</Grid>
</Window>
 //XML文件
1 <?xml version="1.0" encoding="utf-8" ?>
<Person>
<Information>
<Name>张三</Name>
<Age>22</Age>
<Sex>男</Sex>
</Information>
<Information>
<Name>李四</Name>
<Age>25</Age>
<Sex>男</Sex>
</Information>
</Person>
//效果截图

WPF系列


二、保存数据到XML

             XmlDocument xml = new XmlDocument();
string str_path = @"J:\WPF\test\XMLData.xml";
XmlNode root = null;
if (File.Exists(str_path))
{
xml.Load(str_path);
root = xml.SelectSingleNode("Person");
} XmlNodeList nodelist = xml.SelectNodes("/Person/Student/Infor");
XmlElement inf,adr, stu, info, UN, IP, DB;
bool bl_exist = true;
foreach (XmlNode node in nodelist)
{
if (node.ChildNodes[].InnerText == (ServerName.Text + ".").ToString() &&
node.ChildNodes[].InnerText == (UserName.Text + ".").ToString() &&
node.ChildNodes[].InnerText ==DataName.Text)
{
bl_exist = false;
break;
}
}
if (bl_exist)
{
stu = xml.CreateElement("Student"); //创建元素
info = xml.CreateElement("Infor");
UN = xml.CreateElement("UserName");
IP = xml.CreateElement("IP");
DB = xml.CreateElement("DataBaseName"); inf = xml.CreateElement("Information");
adr = xml.CreateElement("Address");
adr.InnerText = (UserName.Text + "." + ServerName.Text + "." + DataName.Text).ToString();
root.AppendChild(inf);
inf.AppendChild(adr); UN.InnerText = (UserName.Text + ".").ToString();
IP.InnerText = (ServerName.Text + ".").ToString();
DB.InnerText = DataName.Text;
stu.AppendChild(info);
info.AppendChild(UN);
info.AppendChild(IP);
info.AppendChild(DB);
root.AppendChild(stu);
MessageBox.Show("数据保存成功");
}
xml.Save(str_path);

三、页面切换出现淡入淡出效果和图表控件页面重新加载动画

1、把Window窗口改为NavigationWindow导航切换窗口,添加源文件Market.xaml
<NavigationWindow x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1080" Width="3840" WindowState="Maximized" Source="Market.xaml" WindowStyle="None" ShowsNavigationUI="False" ResizeMode="NoResize" Navigating="NavigationWindow_Navigating"
MouseLeftButtonDown="NavigationWindow_MouseLeftButtonDown">
</NavigationWindow> 2、下一个页面切换的时候用以下代码就可实现:
NavigationService.Navigate(new Uri("Power.xaml", UriKind.Relative)); 3、淡入淡出效果
        private void NavigationWindow_Navigating(object sender, NavigatingCancelEventArgs e)
{
if (Content != null && !_allowDirectNavigation)
{
e.Cancel = true;
_navArgs = e;
this.IsHitTestVisible = false;
DoubleAnimation da = new DoubleAnimation(0.3d, new Duration(TimeSpan.FromMilliseconds()));
da.Completed += FadeOutCompleted;
this.BeginAnimation(OpacityProperty, da);
}
_allowDirectNavigation = false;
}
private void FadeOutCompleted(object sender, EventArgs e)
{
(sender as AnimationClock).Completed -= FadeOutCompleted; this.IsHitTestVisible = true; _allowDirectNavigation = true;
switch (_navArgs.NavigationMode)
{
case NavigationMode.New:
if (_navArgs.Uri == null)
{
NavigationService.Navigate(_navArgs.Content);
}
else
{
NavigationService.Navigate(_navArgs.Uri);
}
break;
case NavigationMode.Back:
NavigationService.GoBack();
break; case NavigationMode.Forward:
NavigationService.GoForward();
break;
case NavigationMode.Refresh:
NavigationService.Refresh();
break;
} Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
(ThreadStart)delegate()
{
DoubleAnimation da = new DoubleAnimation(1.0d, new Duration(TimeSpan.FromMilliseconds()));
this.BeginAnimation(OpacityProperty, da);
});
}
private bool _allowDirectNavigation = false;
private NavigatingCancelEventArgs _navArgs = null;