DataGridComboBoxColumn

DataGridComboBoxColumn

本文介绍了DataGridComboBoxColumn为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的DataGridComboBoxColumn不显示任何数据。它是空的。
我可以很容易地填充ComboBox,但是DataGridComboBoxColumn不起作用。

.NetFramework 4.6.1

My DataGridComboBoxColumn does not show any data. It's empty.I have no problem to fill a ComboBox, but DataGridComboBoxColumn doesn't work.
.NetFramework 4.6.1

型号:

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

ViewModel:

ViewModel:

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>

此行为的原因是什么?

推荐答案

您正在绑定到 DataGridComboBoxColumn 中的属性 CountryList 。但是此属性不在您的课程地址中;它在您的课程 AddressViewModel 中。这就是为什么您在 ComboBox 中看不到任何内容的原因;

You are binding to a property CountryList in your DataGridComboBoxColumn. But this property is not in your class Address; it's in your class AddressViewModel. Thats why you don't see anything in your ComboBox; the binding is not valid.

您的 List< Address>中的每个条目AddressList 代表 DataGrid 中的一行,您可以访问 Address 中的每个属性每行具有普通绑定 {Binding Property} 。但是您想访问一个类,该类包含 List< Address>地址列表。因此,您必须使用其他方式进行绑定。有两种方法:

Each entry in your List<Address> AddressList stands for one line in your DataGrid and you can access each property of Address in each line with the 'normal' binding {Binding Property}. But you want to access a property which is in the class that holds the List<Address> AddressList. That's why you have to use a different way to bind. Two ways are possible:


  • RelativeSource 绑定

  • 通过 ElementName
  • 绑定
  • RelativeSource binding
  • binding via 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 而不是列表,以使视图保持最新。

And just a hint: if you bind collections to your view use ObservableCollection instead of List to keep your view up to date.

这篇关于DataGridComboBoxColumn为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-19 00:04