首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataGridTemplateColumn ComboBox结合

DataGridTemplateColumn ComboBox结合
EN

Stack Overflow用户
提问于 2014-03-11 10:35:57
回答 2查看 4.9K关注 0票数 2

我有一个绑定到一组员工的DataGrid。Employee类具有类型为Country的EmployeeCountry。国家类型由CountryId和CountryName组成。

我有以下XAML:

代码语言:javascript
复制
     <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属性,ViewModel是包含DataGrid的窗口的DataContext。我能够用ComboBox填充CountryList。

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

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

EN

回答 2

Stack Overflow用户

发布于 2014-03-12 15:33:18

我能够用我在其中一个评论中提到的问题来解决我的关切。只是发布了帮助它正常工作的XAML:

代码语言:javascript
复制
 <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>
票数 2
EN

Stack Overflow用户

发布于 2014-03-11 10:57:12

我会做这样的事

代码语言:javascript
复制
 <ComboBox SelectedValuePath="CountryName" SelectedItem="{Binding Country}"  ItemsSource="{Binding Path=DataContext.CountryList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}">

为了向你们展示我给你们的作品,我把所有的代码都写好了。

代码语言:javascript
复制
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

代码语言:javascript
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22323130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档