2017-03-23 44 views
1

我的DataGridComboBoxColumn不显示任何数据。它是空的。 我没有问题来填充组合框,但DataGridComboBoxColumn不起作用。
.NetFramework 4.6.1DataGridComboBoxColumn为空

型号:

public class Address 
{ 
    public string Firstname { get; set; } 
    public string Lastname { get; set; } 
    public string Country { get; set; } 
} 

视图模型:

public class AddressViewModel 
{ 
    public AddressViewModel() 
    { 
     LoadAdresses(); 
     LoadCountries(); 
    } 

    public List<Address> AddressList { get; set; } 
    public List<string> CountryList { get; set; } 


    private void LoadAdresses() 
    { 
     AddressList = new List<Model.Address>(); 
     AddressList.Add(new Model.Address(){ Firstname = "Peter", Lastname = "R.", Country = "A" }); 
     AddressList.Add(new Model.Address(){ Firstname = "Tom", Lastname = "A.", Country = "A" }); 
     AddressList.Add(new Model.Address(){ Firstname = "Sam", Lastname = "F.", Country = "A" }); 
    } 

    private void LoadCountries() 
    { 
     CountryList = new List<string>(); 
     CountryList.Add("A"); 
     CountryList.Add("D"); 
     CountryList.Add("CH"); 
     CountryList.Add("GB"); 
     CountryList.Add("F"); 
    } 
} 

查看:

<Window.DataContext> 
    <vm:AddressViewModel/> 
</Window.DataContext> 

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <DataGrid x:Name="AddressDataGrid" Grid.Row="0" ItemsSource="{Binding AddressList}" AutoGenerateColumns="False" > 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="Firstname" Binding="{Binding Firstname}" />    
      <DataGridTextColumn Header="Lastname" Binding="{Binding Lastname}" /> 
      <DataGridComboBoxColumn Header="Country" 
         SelectedItemBinding="{Binding Country}" 
         ItemsSource="{Binding CountryList}"/> 
     </DataGrid.Columns> 
    </DataGrid> 

    <!--This ComboBox works--> 
    <ComboBox Grid.Row="1" ItemsSource="{Binding CountryList}"/> 
</Grid> 

什么是这种现象的原因?

回答

1

您绑定到您的DataGridComboBoxColumn中的属性CountryList。但是这个属性不在你的课Address;它在你的班级AddressViewModel。这就是为什么你没有看到你的ComboBox;该绑定无效。

每个条目在List<Address> AddressList代表一行在DataGrid,您可以在每一行与“正常”的结合{Binding Property}访问Address每个属性。但是您想访问持有List<Address> AddressList的课程中的某个属性。这就是为什么你必须使用不同的方式进行绑定。两种方法是可能的:

  • RelativeSource结合
  • 通过ElementName

结合这应该工作,你

<DataGridComboBoxColumn Header="Country" 
         SelectedItemBinding="{Binding Country}"> 
    <DataGridComboBoxColumn.ElementStyle> 
     <Style TargetType="{x:Type ComboBox}"> 
      <Setter Property="ItemsSource" 
        Value="{Binding Path=DataContext.CountryList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" /> 
     </Style> 
    </DataGridComboBoxColumn.ElementStyle> 
    <DataGridComboBoxColumn.EditingElementStyle> 
     <Style TargetType="{x:Type ComboBox}"> 
      <Setter Property="ItemsSource" 
        Value="{Binding Path=DataContext.CountryList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" /> 
     </Style> 
    </DataGridComboBoxColumn.EditingElementStyle> 
</DataGridComboBoxColumn> 

而只是一个提示:如果你绑定的集合到您的视图的使用ObservableCollection而不是List,以使您的视图保持最新状态。

+0

我的DataGrid绑定到列表

AddressList的实例。类地址有一个属性国家。 DataGridComboBoxColumn绑定到此属性,并使用ListStyle CountryList for ItemsSource的实例来生成组合框控件的内容。
其他组合框工作! – PeRa

+0

我知道了,我已经看到你的代码:) 我试图解释为什么你的DataGridComboBox不起作用。这是因为你没有在正确的DataContext中。每行都有一个Address对象作为DataContext,并且在Address对象中没有CountryList。这就是为什么它不起作用。 DataGrid外部的ComboBox具有不同的DataContext。你有没有试过我的xaml代码? –

+0

谢谢。你的xaml工作,我理解你的解释。非常复杂。这意味着,如果我想使用ItemsSource属性,我必须使用StaticResource? – PeRa