无法绑定数据网格中的组合框中的数据

时间:2014-05-27 14:19:15

标签: c# wpf xaml datagrid wpfdatagrid

我在WPF的数据网格中有一个组合框。我无法绑定数据。我使用了以下代码。

XAML

<DataGrid 
   Name="grdDetails" 
   Width="578" 
   Height="149" 
   ScrollViewer.VerticalScrollBarVisibility="Visible" 
   ScrollViewer.HorizontalScrollBarVisibility="Visible" 
   AutoGenerateColumns="False"  
   MouseRightButtonUp="grdDetails_MouseRightButtonUp" 
   SelectionChanged="grdDetails_SelectionChanged">
   <DataGrid.Columns>
      <DataGridTemplateColumn Width="80" Header="Country">
         <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
               <TextBlock Text="{Binding Country}" />
            </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
         <DataGridTemplateColumn.CellEditingTemplate>
            <DataTemplate>
               <ComboBox 
                  Name="cbCountry" 
                  ItemsSource="{Binding Path=CountryList}" 
                  SelectedItem="Code" 
                  DisplayMemberPath="Code" 
                  SelectedValuePath="Code"/>
            </DataTemplate>
         </DataGridTemplateColumn.CellEditingTemplate>
      </DataGridTemplateColumn>
   </DataGrid.Columns>
</DataGrid>

背后的代码(C#)

List<CountryDTO> CountryList = new List<CountryDTO>();
CountryList = refController.GetCountryData();  // this brings the list of Country.

请建议。

4 个答案:

答案 0 :(得分:2)

你真的可以遇到这么多问题,从你向我们展示的那些微小的代码规范来看......没有特别的顺序,请检查以下内容:

1)请确保您的grdDetails DataGridItemsSource属性设置为有效的项目集合,或者DataGrid中没有数据......我假设为了简洁起见,你愚蠢地将其排除在代码示例之外。

2)请确保Binding中的ComboBox在范围内。如果您尝试将数据绑定到单个集合,则此Binding将无效:

<ComboBox Name="cbCountry" ItemsSource="{Binding Path=CountryList}" ... />  

以上Binding Path属于ComboBoxItem的范围,因此要使其工作,您需要为每个数据绑定添加CountryList集合属性要在ComboBox中显示的项目。要使对象中只有一个集合可以使用绑定到父DataContext属性的数据,您需要使用此Binding Path

<ComboBox Name="cbCountry" ItemsSource="{Binding Path=DataContext.CountryList, 
    RelativeSource={RelativeSource AncestorType={x:Type YourPrefix:YourView}}}" ... />

3)在理想情况下,您的数据项应该是ObservableCollection而不是List。来自MSDN上的ObservableCollection<T> Class页:

  

表示动态数据集合,在添加,删除项目或刷新整个列表时提供通知。

4)在WPF中使用数据传输对象(DTO)作为模型项是不明智的,除非它们实现INotifyPropertyChanged接口。即使他们这样做,也几乎总是优先定义您自己的自定义数据类型类,它们提供在UI中编辑和显示所需的所有属性...我们经常需要添加属性有助于在UI中可视化事物。

5)请确保您已将对象的有效实例设置为您正在使用的DataContextWindow的{​​{1}}。

6)请确保您设置为UserControl的对象实际上有数据进入。

虽然 还有其他原因导致您DataContext中没有任何数据,但遗憾的是我已经没时间了...我已经列出了最可能的原因你的问题,但为了将来参考,也许你可以provide all of the relevant information in your question

答案 1 :(得分:1)

使用MVVM,您需要创建一个视图模型类,它看起来如下所示,并在您的代码中将其实例分配给Window的DataContext属性。

然后绑定会这样:

ItemsSource="{Binding Path=CountryList , RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"

视图模型类看起来像这样:

public class MyViewModel
{

    public ObservableCollection<CountryDTO> CountryList { get; private set; }

    public MyViewModel(SomeControler refController)
    {
        CountryList = new ObservableCollection(refController.GetCountryData());
    }
}

答案 2 :(得分:1)

如果在CodeBedind(而不是DataContext)中定义了CountryList,那么您还需要一个RelativeSource:

... ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type YourView}}, Path=CountryList,}" ...

答案 3 :(得分:0)

使用ObservableCollection而不是列表,然后添加的对象应该可用。更多信息:ObservableCollection<> vs. List<>