简体   繁体   English

为datagrid组合框中的每一行添加一个不同的ItemsSource

[英]add a different ItemsSource for every row in a datagrid combobox

I need to declare a list of TextBox values and a ComboBox with some fields in it. 我需要声明一个TextBox值列表和一个包含某些字段的ComboBox。 Each ComboBox in every row might have 3 or 4 or 5 values,depending on what I pull from a database. 每行中的每个ComboBox可能具有3或4或5个值,具体取决于我从数据库中提取的内容。 But when I declare the fields, all the TextBoxes are binded properly, but the last ComboBox is always empty. 但是,当我声明这些字段时,所有的TextBox都正确绑定,但是最后一个ComboBox始终为空。 I didnt add the TexBoxes and their code for clarity. 为了清楚起见,我没有添加TexBoxes及其代码。 XAML: XAML:

<DataGrid AutoGenerateColumns="False" x:Name="dataGrid" ItemsSource="{Binding Finalize_routing}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="249" Width="582">
     <DataGrid.Columns>
        <DataGridComboBoxColumn Header="Stazione" ItemsSource="{Binding Station}" DisplayMemberPath="stazione_id" ></DataGridComboBoxColumn>
     </DataGrid.Columns>

main class: 主类:

Finalize_routing = new ObservableCollection<Stazioni_operazioni>();                
Finalize_routing.Add(new Stazioni_operazioni
            {
                Station = new ObservableCollection<Stazioni> { new Stazioni { stazione_id="1"},new Stazioni { stazione_id="2"} }
            });
        }
        InitializeComponent();
        DataContext = this;

stazioni_operazioni: stazioni_operazioni:

    private ObservableCollection<Stazioni> station;
    public ObservableCollection<Stazioni> Station
    {
        get { return station; }
        set
        {
            if (station != value)
            {
                station = value;

            }
        }
    }

stazioni: 斯塔齐奥尼:

public class Stazioni{
    public string stazione_id { get; set; }
}

there was some problem with the class "stazioni", didnt figure out why.anyway i used a list of strings instead “ stazioni”类存在一些问题,没有弄清楚为什么。反正我使用了一个字符串列表

final_route={"1","2","3"};
Finalize_routing.Add(new Stazioni_operazioni
    {
    Station = final_route
});

and xaml: 和xaml:

            <DataGridTemplateColumn Header="Stazione" Width="80">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Path=Station,Mode=TwoWay}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 组合框将ItemsSource绑定到与DataGrid相同的对象时,无法向DataGrid添加新行 - Can't add new row to DataGrid, while it has combobox that bind ItemsSource to same object as DataGrid 在DataGrid RowDetailsTemplate中绑定ComboBox ItemsSource - Binding ComboBox ItemsSource in DataGrid RowDetailsTemplate WPF不同项组合框的源 - WPF Different ItemsSource for a Combobox WPF DataGrid - 从 DataGrid ItemsSource 对象的集合值中设置每行(对象)唯一的 combobox 值 - WPF DataGrid - Set unique per row (object) combobox values from the DataGrid ItemsSource object's collection value WPF每行都有不同的ComboBox ItemsSource。 来自数据库的数据 - WPF Different ComboBox ItemsSource for each row. Data from Database c#向datagrid组合框添加行 - c# add row to a datagrid combobox 在ComboBox的SelectionChanged事件上更新DataGrid的ItemsSource - Update DataGrid's ItemsSource on SelectionChanged event of a ComboBox 如何更改DataGrid内部的ComboBox的ItemsSource? - How to change ItemsSource of ComboBox inside DataGrid? 如何在 UWP 项目中将 comboBox 绑定到与其父 DataGrid 的 ItemSource 不同的 ItemsSource? - How do I bind a comboBox to a different ItemsSource than it's parent DataGrid's ItemSource in a UWP project? 以编程方式将绑定添加到DataGrid的ItemsSource - Programmatically add binding to ItemsSource of DataGrid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM