out x) double.TryParse(arg2

时间:2022-05-11 07:35:54

使用调集东西作为列表控件的ItemSource

前台:

<ListBox x:Name="listBoxStudent" >

<ListBox.ItemTemplate>

<DataTemplate>

<StackPanel Orientation="Horizontal">

<TextBlock Text="{Binding StudentID}"></TextBlock>

<TextBlock Text="{Binding StudentName}"/>

<TextBlock Text="{Binding StudentAge}"/>

</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" DisplayMemberBinding="{Binding Id}"></GridViewColumn>

<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>

<GridViewColumn Header="Age" 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>

<ListView.View>

<GridView>

<GridViewColumn Header="Id" DisplayMemberBinding="{Binding [email protected]}"/>

<GridViewColumn Header="Name" DisplayMemberBinding="{Binding XPath=Name}"/>

<GridViewColumn Header="Age" 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>

<Name>Tim</Name>

<Age>29</Age>

</Student>

<Student>

<Name>Tom</Name>

<Age>28</Age>

</Student>

<Student>

<Name>Tony</Name>

<Age>27</Age>

</Student>

<Student>

<Name>Kyle</Name>

<Age>26</Age>

</Student>

<Student>

<Name>Vina</Name>

<Age>25</Age>

</Student>

<Student>

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

<Folder>

<Folder>

<Folder/>

<Folder/>

<Folder/>

</Folder>

</Folder>

<Folder>

<Folder/>

<Folder/>

<Folder/>

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

功效: