当前位置: 移动技术网 > IT编程>开发语言>.net > WPF(Binding of ItemsControl)

WPF(Binding of ItemsControl)

2018年11月03日  | 移动技术网IT编程  | 我要评论

磨损胸中万古刀,神犬小巴迪国语,足球资讯网301099

[html] 
<window x:class="testofbindingitemscontrol.mainwindow" 
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" 
        title="mainwindow" height="350" width="525"> 
    <stackpanel x:name="stackpanel" background="lightblue" > 
        <textblock text="studentid:" margin="5" /> 
        <textbox x:name="textboxid" margin="5" /> 
        <textblock text="studentlist:" fontweight="bold" 
                   margin="5" /> 
        <listbox x:name="listboxstudents"  
                 height="110"  
                 margin="5" 
                 > 
            <listbox.itemtemplate> 
                <datatemplate> 
                    <stackpanel orientation="horizontal" > 
                        <textblock text="{binding path=id}" width="30" /> 
                        <textblock text="{binding path=name}" width="60" /> 
                        <textblock text="{binding path=age}" width="30" />                     
                    </stackpanel> 
                </datatemplate> 
            </listbox.itemtemplate> 
        </listbox> 
         
    </stackpanel> 
</window> 

<window x:class="testofbindingitemscontrol.mainwindow"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        title="mainwindow" height="350" width="525">
    <stackpanel x:name="stackpanel" background="lightblue" >
        <textblock text="studentid:" margin="5" />
        <textbox x:name="textboxid" margin="5" />
        <textblock text="studentlist:" fontweight="bold"
                   margin="5" />
        <listbox x:name="listboxstudents"
                 height="110"
                 margin="5"
                 >
            <listbox.itemtemplate>
                <datatemplate>
                    <stackpanel orientation="horizontal" >
                        <textblock text="{binding path=id}" width="30" />
                        <textblock text="{binding path=name}" width="60" />
                        <textblock text="{binding path=age}" width="30" />                   
                    </stackpanel>
                </datatemplate>
            </listbox.itemtemplate>
        </listbox>
       
    </stackpanel>
</window>
[csharp] 
using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.windows; 
using system.windows.controls; 
using system.windows.data; 
using system.windows.documents; 
using system.windows.input; 
using system.windows.media; 
using system.windows.media.imaging; 
using system.windows.navigation; 
using system.windows.shapes; 
 
namespace testofbindingitemscontrol 

    /// <summary>  
    /// interaction logic for mainwindow.xaml  
    /// </summary>  
    public partial class mainwindow : window 
    { 
        public mainwindow() 
        { 
            initializecomponent(); 
 
            list<student> stulist = new list<student>() 
            { 
                  new student(){id = 0,name = "tim",age = 29}, 
                  new student(){id = 1,name = "tom",age = 28}, 
                  new student(){id = 2,name = "kyle",age = 27}, 
                  new student(){id = 3,name = "tony",age = 26}, 
                  new student(){id = 4,name = "vine",age = 25}, 
                  new student(){id = 5,name = "mike",age = 24}      
            }; 
 
            // 为listbox设置binding  
            this.listboxstudents.itemssource = stulist; 
            //this.listboxstudents.displaymemberpath = "name";  
 
            binding binding = new binding("selecteditem.id") 
                                  { 
                                      source = this.listboxstudents 
                                  }; 
 
            this.textboxid.setbinding(textbox.textproperty, binding); 
        } 
    } 
 
    public class student 
    { 
        public int id { get; set; } 
 
        public string name { get; set; } 
 
        public int age { get; set; } 
    } 

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.windows;
using system.windows.controls;
using system.windows.data;
using system.windows.documents;
using system.windows.input;
using system.windows.media;
using system.windows.media.imaging;
using system.windows.navigation;
using system.windows.shapes;

namespace testofbindingitemscontrol
{
    /// <summary>
    /// interaction logic for mainwindow.xaml
    /// </summary>
    public partial class mainwindow : window
    {
        public mainwindow()
        {
            initializecomponent();

            list<student> stulist = new list<student>()
            {
                  new student(){id = 0,name = "tim",age = 29},
                  new student(){id = 1,name = "tom",age = 28},
                  new student(){id = 2,name = "kyle",age = 27},
                  new student(){id = 3,name = "tony",age = 26},
                  new student(){id = 4,name = "vine",age = 25},
                  new student(){id = 5,name = "mike",age = 24}    
            };

            // 为listbox设置binding
            this.listboxstudents.itemssource = stulist;
            //this.listboxstudents.displaymemberpath = "name";

            binding binding = new binding("selecteditem.id")
                                  {
                                      source = this.listboxstudents
                                  };

            this.textboxid.setbinding(textbox.textproperty, binding);
        }
    }

    public class student
    {
        public int id { get; set; }

        public string name { get; set; }

        public int age { get; set; }
    }
}

 

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网