DataGridTemplateColumn ComboBox绑定

时间:2014-03-11 10:35:57

标签: wpf data-binding combobox datagridtemplatecolumn

我有一个绑定到Employees集合的DataGrid。 Employee类具有Country类型的EmployeeCountry。国家/地区类型包含CountryId和CountryName。

我有以下XAML:

     <DataGrid ItemsSource="{Binding EmployeeList}" CanUserAddRows="True">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="CountryCombo2">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Path=DataContext.CountryList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
                                      DisplayMemberPath="CountryName" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

EmployeeList和CountryList是ViewModel上的ObservableCollection属性,它是包含DataGrid的Window的DataContext。我可以使用CountryList填充ComboBox。

问题:我需要弄清楚如何设置ComboBox的其他属性,如SelectedValuePath,SelectedItem等,以便DataGrid的每一行在ComboBox中正确显示相应的EmployeeCountry。如果Employee的EmployeeCountry属性为NULL,则ComboBox不应选择任何项目。

更新:即使CanUserAddRows属性设置为true,我也无法向DataGrid添加新行。

2 个答案:

答案 0 :(得分:2)

我能够使用我的一条评论中提到的问题来解决我的问题。只需发布有助于其运作的XAML:

 <DataGrid ItemsSource="{Binding EmployeeList}" CanUserAddRows="True" AutoGenerateColumns="False" Margin="0,0,0,90">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="CountryCombo2">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Path=DataContext.CountryList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
                                      DisplayMemberPath="CountryName" 
                                      SelectedItem="{Binding EmployeeCountry, Mode=TwoWay}"
                                      SelectedValue="{Binding EmployeeCountry.CountryId}"
                                      SelectedValuePath="CountryId" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

答案 1 :(得分:1)

我会做这样的事情

 <ComboBox SelectedValuePath="CountryName" SelectedItem="{Binding Country}"  ItemsSource="{Binding Path=DataContext.CountryList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}">

为了告诉你我给你的作品,我正在编写完整的代码。

public class Employees :INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    private string _name;
    public string Name
    {
        get
        {
            return _name; 
        }

        set
        {
            if (_name == value)
                return;
            _name = value;
            OnPropertyChanged();
        }
    }

    private Country _employeeCountry;
    public Country EmployeeCountry
    {
        get
        {
            return _employeeCountry;
        }

        set
        {
            if (_employeeCountry == value)
                return;
            _employeeCountry = value;
            OnPropertyChanged();
        }
    }
}

public class Country
{
    private string _name;
    public string Name
    {
        get
        {
            return _name;
        }

        set
        {
            if (_name == value)
                return;
            _name = value;
        }
    }
}

private static ObservableCollection<Country> _countryList = new ObservableCollection<Country>(new []{ new Country{Name="US"}, new Country{Name="UK"}});

public ObservableCollection<Country> CountryList
{
    get
    {
        return _countryList;
    }
}

private ObservableCollection<Employees> _employeeList = new ObservableCollection<Employees>(new[] { new Employees { Name = "Ty", EmployeeCountry = _countryList.First() }, new Employees { Name = "Dude" } });

public ObservableCollection<Employees> EmployeeList
{
    get
    {
        return _employeeList;
    }
}

和Xaml

<DataGrid ItemsSource="{Binding EmployeeList}" CanUserAddRows="True">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="CountryCombo2">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Path=CountryList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" DisplayMemberPath="Name"
                                 SelectedValuePath="Name" SelectedItem="{Binding EmployeeCountry}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>