0

我的网格:

 <dg:DataGrid Grid.Column="0" Grid.Row="0" Name="recipe_grid" ItemsSource="{Binding}" AutoGenerateColumns="False" 
                    IsReadOnly="True" HeadersVisibility="Column" FontSize="14" FontFamily="Arial Narrow" FontWeight="Bold"
                    Background="Transparent" RowBackground="Transparent" BorderBrush="DarkGray"
                    CanUserDeleteRows="False" CanUserAddRows="False" CanUserResizeRows="False" ColumnHeaderStyle="{DynamicResource ColumnHeaderStyle}" BorderThickness="0" VerticalGridLinesBrush="#FF6C6464" HorizontalGridLinesBrush="#016C6464">
                        <dg:DataGrid.Columns>
                            <dg:DataGridTextColumn Binding="{Binding Path=name}" Header="name"/>
                            <dg:DataGridTemplateColumn Header="country" CanUserReorder="False" CanUserSort="False" >
                                <dg:DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <ComboBox ItemsSource="{Binding Source={StaticResource CountryListData}}"  DisplayMemberPath="Name" SelectedValue="idcountry" Name="combo_country"></ComboBox>

                                    </DataTemplate>
                                </dg:DataGridTemplateColumn.CellTemplate>
                            </dg:DataGridTemplateColumn>

                        </dg:DataGrid.Columns>
                    </dg:DataGrid>

资源:

 <local:CountryList x:Key="CountryListData"/>

代码: 主要:

 DataTable car = new DataTable();
       car.Columns.Add(new DataColumn("id_car", typeof(int)));
       car.Columns.Add(new DataColumn("name", typeof(string)));
       car.Columns.Add(new DataColumn("id_country", typeof(int)));

国家:

 public class CountryList : ObservableCollection<CountryName>
{
    public CountryList(): base()
    {
        NpgsqlConnection conn = new NpgsqlConnection("Server=localhost; Port=5432; UserId=postgres; Password=whatever; Database=backup");
        conn.Open();
        DataSet CL = new DataSet();
        DataTable country = new DataTable();
        country.Columns.Add(new DataColumn("id_country", typeof(int)));
        country.Columns.Add(new DataColumn("name", typeof(string)));

        CL.Tables.Add(country);
        NpgsqlDataAdapter country_fill = new NpgsqlDataAdapter();
        country_fill = new NpgsqlDataAdapter();
        country_fill.SelectCommand = new NpgsqlCommand();
        country_fill.SelectCommand.Connection = conn;
        country_fill.SelectCommand.CommandText = "SELECT * FROM country";
        country_fill.Fill(country);
        conn.Close();           


        foreach (DataRow row in country.Rows)
        {
            Add(new CountryName((string)row.ItemArray[1], (int)row.ItemArray[0]));
        }
    }
}

public class CountryName : INotifyPropertyChanged
{
    public string name;
    public int id_country;
    public event PropertyChangedEventHandler PropertyChanged;

    public CountryName(string country_name, int id)
    {
        this.name = country_name;
        this.id_country = id;
    }

    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            OnPropertyChanged("CountryName");
        }
    }

    public int idcountry
    {
        get { return id_country; }
        set { id_country = value; }
    }
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

我没有发布所有代码,但您可以理解。我需要在装满汽车的 DataGrid 中显示 Country 的名称而不是 id_country。正如您在这一点上看到的那样,我所拥有的只是一个绑定到显示所有汽车的 DataGrid 的 DataTable 和一个包含国家/地区的 ObservableCollection。在 WinForms 中,一切都很简单。但是现在我只做到了这一点(在这个社区的大力帮助下)。如何使 ComboBox 仅显示一个值,该值将是每辆车的国家/地区的正确名称?

4

0 回答 0