【问题标题】:How to add WPF DataGridTemplateColumn.CellEditingTemplate ComboBox itemsource at back-end如何在后端添加 WPF DataGridTemplateColumn.CellEditingTemplate ComboBox itemssource
【发布时间】:2016-09-08 00:53:04
【问题描述】:

我想将我的 DataTable 返回数据添加到我的 DataGridTemplateColumn.CellEditingTemplate 组合框,但组合框是空的

我的 xaml 代码如下

 <DataGridTemplateColumn Header="Student">
   <DataGridTemplateColumn.CellEditingTemplate >
      <DataTemplate>
           <ComboBox ItemsSource="{Binding dtStudent}"    DisplayMemberPath="StudentName"/>
      </DataTemplate>
   </DataGridTemplateColumn.CellEditingTemplate>
 </DataGridTemplateColumn>

后端

dtStudent = new Function().Sel_Student(); //Function that return student detail

我尝试为组合框控件添加名称,但无法从后端访问。

我的绑定方法是错误的还是我有其他方法来实现我需要的东西

【问题讨论】:

    标签: wpf data-binding combobox wpfdatagrid


    【解决方案1】:

    在 DataTemplate 中定义控件时,无法通过其名称访问该控件。您可以从其可视化树中获取 ComboBox。 请参考这个WPF How to access control from DataTemplate

    【讨论】:

      【解决方案2】:

      您需要绑定到属性而不是函数。所以在你的后端你应该有

      public IEnumerable<Student> Students
      {
          get
          {
              return Sel_Students();
          }
      }
      
      private IEnumerable<Student> Sel_Students()
      {
          return { ... your students};
      }
      
      public class Student {
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-30
        • 2015-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多