Bindings for a ListBox in WPF

Source: Internet
Author: User

A list-style control in WPF derives from the ItemsControl class and inherits the ItemsSource property. The ItemsSource property can receive an instance of a IEnumerable interface derived class as its own value (all collections that can be iterated through are implemented with this interface, such as arrays, list<t>, and so on). Each itemscontrol derived class has its own item container, An entry container such as a ListBox ListBoxItem. When we use the binding to set the ItemsSource property value for a ItemsControl, the ItemsControl object automatically iterates over the data element and prepares an entry container for each data element.
In the following example, a data source of type list<t> is bound to a ListBox and the ID of the selected student object is displayed in the Write box.

The interface effect is as follows:

XAML File code:

[HTML]View PlainCopyprint?
  1. <window x:class="_6_15.mainwindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"
  4. title="MainWindow" height= "width=" 525 ">
  5. <Grid>
  6. <ListBox height= "164" horizontalalignment="left" margin="12,0,0,12" name= "ListBox1" verticalalignment="Bottom" width="471 "
  7.                   displaymemberpath= "Name"   selectedvaluepath= "ID" />   
  8. <TextBox height= "horizontalalignment=" "left " margin="12,61,0,0" name= "TextBox1" verticalalignment="Top" width="
  9. text="{Binding selecteditem.id,elementname=listbox1}"/>
  10.         <TextBlock  height= "all"   Horizontalalignment= "left"  margin=" 12,32,0,0 " name=" TextBlock1 "  text= "Student id:"   verticalalignment= "Top"  />   
  11. <TextBlock height="horizontalalignment=" "left " margin="12,106,0,0" name= "TextBlock2" text="Student List:" verticalalignment="Top" />
  12. </Grid>
  13. </Window>

What you need to illustrate here is the DisplayMemberPath property of the ListBox, as its name implies, its function is the path of the bound object that needs to be shown in the ListBox, and SelectedValuePath, This means that when we select an item we can get the type of the value through the SelectedValue property of the ListBox, if Zhang San is selected, we can get the Zhang San ID by SelectedValue.
Each class derived from the ItemsControl class has the above attributes, including the ListView, ListBox, Combox, TreeView, and so on.

[CSharp]View PlainCopyprint?
    1. Public partial class Mainwindow:window
    2. {
    3. Public MainWindow ()
    4. {
    5. InitializeComponent ();
    6. list<student> stulist = new list<student> ()
    7. {
    8. New Student () {id=1,name="Zhang Three ¨y"},
    9. New Student () {id=2,name="LI ¤ four?"},
    10. New Student () {id=3,name="Wang a five?"}
    11. };
    12. This.listBox1.ItemsSource = stulist;
    13. }
    14. }
    15. public class Student
    16. {
    17. public int ID { get;  set;}
    18. public string Name { get;  set;}
    19. }

Bindings for a ListBox in WPF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.