一种更简单的方法来使用DataGridComboBoxColumn?

时间:2013-05-07 20:51:23

标签: wpf wpfdatagrid datagridcomboboxcolumn

我有一个来自DataGrid的{​​{1}}支持的WPF(.NET 4)DataView,其列为DataTable列。一切都在为我工作,但我必须创建一个新类来保存简单的[DataGridComboBoxColumn / ArrayList /无论我可以尝试的List<string>的内容,以获取可能的值DGCBC似乎应该有一种不太复杂的方式。这是我的XAML:

IEnumerable

我的代码背后:

<Window x:Class="MASTableMaint.TruckloadLimitsEdit"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Edit Truckload Limits" Height="600" Width="460" MinWidth="300" MinHeight="300"  WindowStartupLocation="CenterOwner" >
    <DockPanel LastChildFill="True">
        <DataGrid Name="TruckloadGrid" ItemsSource="{Binding}" CanUserReorderColumns="False" CanUserResizeRows="False" CanUserSortColumns="False"
                  AutoGenerateColumns="False" RowHeaderWidth="20">
            <DataGrid.Columns>
                <DataGridComboBoxColumn x:Name="companyIdColumn" Header="Company" SelectedValueBinding="{Binding Path=CompanyID}" 
                    DisplayMemberPath="TheCompanyID" SelectedValuePath="TheCompanyID"/>
                ...
            </DataGrid.Columns>
        </DataGrid>
    </DockPanel>
</Window>

1 个答案:

答案 0 :(得分:0)

似乎所有行都共享该集合。

如果是这样,请在窗口上定义类型为ObservableCollection<MassCompany>的依赖项属性(我们称之为WinMassCompanies)。

然后在构造函数中:

WinMassCompanies = new ObservableCollection<MassCompany>();
foreach (string company in massCompanies)
{
    WinMassCompanies.Add(new MassCompany(company));
}
//don't fiddle with the column

在XAML中:

<DataGridComboBoxColumn ItemsSource="{Binding WinMassCompanies,
                                              RelativeSource={RelativeSource Mode=FindAncestor,
                                                                             AncestorType={x:Type Window}}}" 
...../>