Wpf binding 学习
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Wpf binding 学习相关的知识,希望对你有一定的参考价值。
-
使用集合对象作为列表控件的ItemSource
前台:
<ListBox x:Name="listBoxStudent" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding StudentID}" Width="30"></TextBlock>
<TextBlock Text="{Binding StudentName}" Width="30"/>
<TextBlock Text="{Binding StudentAge}" Width="30"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
后台:
List<Student> stuList = new List<Student>()
{
new Student(){StudentID=0,StudentName="崔一",StudentAge=27},
new Student(){StudentID=1,StudentName="沈二",StudentAge=27},
new Student(){StudentID=2,StudentName="张三",StudentAge=18},
new Student(){StudentID=3,StudentName="李四",StudentAge=19},
new Student(){StudentID=4,StudentName="王五",StudentAge=20},
};
this.item_List.ItemsSource = stuList;
this.item_List.DisplayMemberPath = "StudentName";
Binding binding = new Binding("SelectedItem.StudentID") { Source = this.item_List };
this.tb_List.SetBinding(TextBox.TextProperty, binding);
listBoxStudent.ItemsSource = stuList;
2.使用ADO.NET对象作为Binding对象
前台:
<ListView x:Name="listStudentView">
<ListView.View>
<GridView>
<GridViewColumn Header="Id" Width="60" DisplayMemberBinding="{Binding Id}"></GridViewColumn>
<GridViewColumn Header="Name" Width="60" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Age" Width="60" DisplayMemberBinding="{Binding Age}"/>
</GridView>
</ListView.View>
</ListView>
后台:
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("Id");
DataColumn dc2 = new DataColumn("Name");
DataColumn dc3 = new DataColumn("Age");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
DataRow dr1 = dt.NewRow();
dr1["Id"] = "1";
dr1["Name"] = "Tim";
dr1["Age"] = "29";
dt.Rows.Add(dr1);
DataRow dr2 = dt.NewRow();
dr2["Id"] = "2";
dr2["Name"] = "Tom";
dr2["Age"] = "28";
dt.Rows.Add(dr2);
DataRow dr3= dt.NewRow();
dr3["Id"] = "3";
dr3["Name"] = "Tony";
dr3["Age"] = "27";
dt.Rows.Add(dr3);
DataRow dr4 = dt.NewRow();
dr4["Id"] = "4";
dr4["Name"] = "Kyne";
dr4["Age"] = "26";
dt.Rows.Add(dr4);
DataRow dr5 = dt.NewRow();
dr5["Id"] = "5";
dr5["Name"] = "Vina";
dr5["Age"] = "25";
dt.Rows.Add(dr5);
DataRow dr6 = dt.NewRow();
dr6["Id"] = "6";
dr6["Name"] = "Emily";
dr6["Age"] = "24";
dt.Rows.Add(dr6);
listStudentView.ItemsSource = dt.DefaultView;
listStudentView.DisplayMemberPath = "Name";
3. 使用Xml作为数据源绑定
前台:
<GroupBox Header="使用Xml作为Binding数据源">
<StackPanel >
<ListView Name="XmlListView">
<ListView.View>
<GridView>
<GridViewColumn Header="Id" Width="80" DisplayMemberBinding="{Binding [email protected]}"/>
<GridViewColumn Header="Name" Width="80" DisplayMemberBinding="{Binding XPath=Name}"/>
<GridViewColumn Header="Age" Width="80" DisplayMemberBinding="{Binding XPath=Age}"/>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</GroupBox>
后台:
XmlDataProvider xdp = new XmlDataProvider();
xdp.Source = new Uri(AppDomain.CurrentDomain.BaseDirectory + "StudentInfo.xml");
xdp.XPath = @"/StudentList/Student";
XmlListView.DataContext = xdp;
this.XmlListView.SetBinding(ListView.ItemsSourceProperty, new Binding());
Xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<StudentList>
<Student Id="1">
<Name>Tim</Name>
<Age>29</Age>
</Student>
<Student Id="2">
<Name>Tom</Name>
<Age>28</Age>
</Student>
<Student Id="3">
<Name>Tony</Name>
<Age>27</Age>
</Student>
<Student Id="4">
<Name>Kyle</Name>
<Age>26</Age>
</Student>
<Student Id="5">
<Name>Vina</Name>
<Age>25</Age>
</Student>
<Student Id="6">
<Name>Emily</Name>
<Age>24</Age>
</Student>
</StudentList>
注意:[email protected]和Xpath=Name的区别,使用@符号加字符串表示的是xml元素的Attribute,不加表示子级元素
将XML和XmlDataProvider写在window.resources中
如:
<Window.Resources>
<XmlDataProvider x:Key="xmlPro" XPath="FileSystem/Folder">
<x:XData>
<FileSystem xmlns="">
<Folder Name="Book">
<Folder Name="Programming">
<Folder Name="Windows">
<Folder Name="Wpf"/>
<Folder Name="Mfc"/>
<Folder Name="Delphi"/>
</Folder>
</Folder>
<Folder Name="Tools">
<Folder Name="Development"/>
<Folder Name="Designment"/>
<Folder Name="Players"/>
</Folder>
</Folder>
</FileSystem>
</x:XData>
</XmlDataProvider>
</Window.Resources>
使用:
<GroupBox Header="XML数据源和XmlDataProvider对象直接写在XAML中">
<TreeView ItemsSource="{Binding Source={StaticResource xmlPro}}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding XPath=Folder}">
<TextBlock Text="{Binding [email protected]}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</GroupBox>
结果:
4. 使用ObjeckDataProvider对象最为Binding的source
代码:
class Calculator
{
public string Add(string arg1, string arg2)
{
double x = 0;
double y = 0;
double z = 0;
if (double.TryParse(arg1, out x) && double.TryParse(arg2, out y))
{
z = x + y;
return z.ToString();
}
return "Input Error";
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ObjectDataProvider odp = new ObjectDataProvider();
odp.ObjectInstance = new Calculator();
odp.MethodName = "Add";
odp.MethodParameters.Add("100");
odp.MethodParameters.Add("200");
MessageBox.Show(odp.Data.ToString());
}
5.Linq绑定数据源
Xml:
<?xml version="1.0" encoding="utf-8" ?>
<StudentList>
<Class>
<Student Id="1" Name="Tom" Age="28"/>
<Student Id="2" Name="Mess" Age="27"/>
</Class>
<Class>
<Student Id="3" Name="Tony" Age="26"/>
<Student Id="4" Name="Vina" Age="25"/>
<Student Id="5" Name="Emily" Age="24"/>
</Class>
</StudentList>
代码绑定:
XDocument xdoc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + @"XmlStudentInfo.xml");
this.ObjectStudentList.ItemsSource = from element in xdoc.Descendants("Student")
where element.Attribute("Name").Value.StartsWith("T")
select new Student()
{
StudentID = int.Parse(element.Attribute("Id").Value),
StudentName = element.Attribute("Name").Value,
StudentAge = int.Parse(element.Attribute("Age").Value)
};
以上是关于Wpf binding 学习的主要内容,如果未能解决你的问题,请参考以下文章