1. 程式人生 > >Wpf binding 學習

Wpf binding 學習

sig evel phi sign column nta istview vid delphi

  1. 使用集合對象作為列表控件的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 XPath=@Id}"/>

<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>

註意:Xpath=@Id和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 XPath=@Name}"/>

</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 學習