开发者社区 问答 正文

WPF数据绑定

谁能指出我一个好的资源(或给我一个提示),向我展示如何在WPF中对控件(ComboBox,ListBox等)进行数据绑定?当我把所有WinForms优点都从我身边拿走时,我有点茫然,而从一开始我就不是那么聪明...

展开
收起
游客ufivfoddcd53c 2020-01-03 16:57:09 931 分享 版权
1 条回答
写回答
取消 提交回答
  • 在后面的代码中-将列表框的DataContext设置为与绑定到的集合相等。

    private void OnInit(object sender, EventArgs e)
    {
      //myDataSet is some IEnumerable 
    
      // myListBox is a ListBox control.
      // Set the DataContext of the ListBox to myDataSet
      myListBox.DataContext = myDataSet;
    }
    
    

    在XAML中,Listbox可以使用“ Binding”语法声明其绑定到的属性。

    <ListBox Name="myListBox" Height="200"
      ItemsSource="{Binding Path=BookTable}"
      ItemTemplate  ="{StaticResource BookItemTemplate}"/>
    
    2020-01-03 16:57:32
    赞同 展开评论
问答分类:
C#
问答标签:
问答地址: