繁体   English   中英

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

[英]Unable to bind data in combo box in datagrid

我在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.

请指教。

根据您向我们展示的那小段代码规范,您确实可能有很多问题……(不按特定顺序排列),请检查以下内容:

1)请确保您的grdDetails DataGridItemsSource属性设置为有效的项目集合,否则DataGrid中将没有数据...我为简洁起见,假设您愚蠢地将其遗漏在代码示例中。

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

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

上面的Binding PathComboBoxItem的范围内,因此,要进行此工作,您需要向每个要在ComboBox显示的数据绑定项添加CountryList集合属性。 要使此功能仅与对象中绑定到父DataContext属性的数据中的一个集合一起使用,您需要使用此Binding Path

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

3)在理想的世界中,您的数据项应位于ObservableCollection而不是List 从MSDN上的ObservableCollection<T>页面:

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

4)将数据传输对象(DTO)用作WPF中的模型项是不明智的,除非它们实现了INotifyPropertyChanged接口。 即使它们做到了,几乎总是最好定义自己的自定义数据类型类,以提供在UI中进行编辑和显示所需的所有属性...我们经常需要添加属性以帮助在UI中可视化事物。

5)请确保已将对象的有效实例设置为所使用的WindowUserControlDataContext

6)请确保设置为DataContext对象实际上有数据进入其中。

虽然仍然进一步的原因,您可能没有在您的任何数据ComboBox ,我很遗憾运行的时间......我已经列出您的问题的最可能的原因,但以供将来参考,也许你可以提供所有您的问题中有哪些相关信息

使用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());
    }
}

如果CountryList是在CodeBedind(而不是DataContext)中定义的,则您还将需要一个RelativeSource:

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

使用ObservableCollection,而不是列表,则添加的对象应该可用。 此处更多信息: ObservableCollection <>与List <>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM