0

我有一个由 a 支持的 WPF (.NET 4) ,DataGrid它的列之一是 a 。一切都对我有用,但我必须创建一个新类来保存 DGCBC 的可能值的简单 [ // whatever I can try] 的内容,当看起来应该不那么复杂时方法。这是我的 XAML:DataViewDataTableDataGridComboBoxColumnArrayListList<string>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>

而我背后的代码:

namespace MASTableMaint
{
    public partial class TruckloadLimitsEdit : Window
    {
        private DataTable truckloadDT = new DataTable();
        private SqlDataAdapter truckloadDA;
        private SqlConnection sqlconn;


        public TruckloadLimitsEdit(List<string> massCompanies)
        {
            sqlconn = new SqlConnection(Constants.SqlConnectionStr);

            if (!DbUtils.OpenDbConn(sqlconn))
            {
                sqlconn.Dispose();
                return;
            }

            string query = "SELECT * FROM tTruckload ORDER BY CompanyID";
            truckloadDA = new SqlDataAdapter(query, sqlconn);
            truckloadDA.Fill(truckloadDT);
            SqlCommandBuilder builder = new SqlCommandBuilder(truckloadDA);

            InitializeComponent();

            DataView truckloadDV = new DataView(truckloadDT, null, null, DataViewRowState.CurrentRows);
            TruckloadGrid.ItemsSource = truckloadDV;

            ObservableCollection<MassCompany> ocMassCompanies = new ObservableCollection<MassCompany>();

            foreach (string company in massCompanies)
            {
                ocMasCompanies.Add(new MassCompany(company));
            }
            companyIdColumn.ItemsSource = ocMassCompanies;
        }
        ...
    }


    public class MassCompany
    {
        public MassCompany(string company)
        {
            TheCompanyID = company;
        }

        public string TheCompanyID { get; set; }
    }
}
4

1 回答 1

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}}}" 
...../>
于 2013-05-07T21:07:51.153 回答