如何创建带有4个组合框和一个标签的数据网格?

时间:2018-10-22 09:52:53

标签: c# wpf observablecollection wpfdatagrid

我正在尝试构建WPF应用程序。在此WPF应用程序中,我具有5页的Tab控件。在其中一个选项卡项中,我创建了一个数据网格。 我有一个函数,可以根据计算出的值来计算一些值。该计算值是一个数字,例如55。

我的Datagrid(XAML):

<DataGrid Name="dg"  Height="2560" Width="1900" Margin="0,10,20,-10" Grid.Row="1">
                            <DataGrid.Columns>
                                <DataGridTemplateColumn Header="Stekker testkast">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <ComboBox ItemsSource="{Binding Path=stekker1}"></ComboBox>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn >
                                <DataGridTemplateColumn Header="Aansluitpin testkast">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <ComboBox ItemsSource="{Binding Path=aansluitpin1}"></ComboBox>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn >
                                <DataGridTextColumn Header="ID" Binding="{Binding Path=id}"></DataGridTextColumn>
                                <DataGridTemplateColumn Header="Stekker testpaneel">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <ComboBox ItemsSource="{Binding Path=stekker2}"></ComboBox>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn >
                                <DataGridTemplateColumn Header="Aansluitpin testpaneel" >
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <ComboBox ItemsSource="{Binding Path=aansluitpin2}"></ComboBox>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn >


                            </DataGrid.Columns>
                        </DataGrid>

如您所见,我有5个列,其中有4个组合框和1个标签作为模板。我该如何做到这样55个这样的模板被添加到数据网格中,并且例如在数字更改时也可以动态添加?

此刻,我正在使用以下代码:

public class Data
    {
        public int id { get; set; }
        public ObservableCollection<int> stekker1 { get; set; }
        public ObservableCollection<int> aansluitpin1 { get; set; }
        public ObservableCollection<int> stekker2 { get; set; }
        public ObservableCollection<int> aansluitpin2 { get; set; }
    }

ObservableCollection<Data> store= new ObservableCollection<Data>();

public MainWindow()
        {
            InitializeComponent();
            store.Add(new Data { id = 1, stekker1 = new ObservableCollection<int> { 11, 2, 33, 22 }, aansluitpin1 = new ObservableCollection<int> { 11, 2, 33, 22 }, stekker2 = new ObservableCollection<int> { 11, 2, 33, 22 }, aansluitpin2 = new ObservableCollection<int> { 11, 2, 33, 22 } });
            store.Add(new Data { id = 2, stekker1 = new ObservableCollection<int> { 11, 2, 33, 22 }, aansluitpin1 = new ObservableCollection<int> { 11, 2, 33, 22 }, stekker2 = new ObservableCollection<int> { 11, 2, 33, 22 }, aansluitpin2 = new ObservableCollection<int> { 11, 2, 33, 22 } });
            store.Add(new Data { id = 3, stekker1 = new ObservableCollection<int> { 11, 2, 33, 22 }, aansluitpin1 = new ObservableCollection<int> { 11, 2, 33, 22 }, stekker2 = new ObservableCollection<int> { 11, 2, 33, 22 }, aansluitpin2 = new ObservableCollection<int> { 11, 2, 33, 22 } });
            store.Add(new Data { id = 4, stekker1 = new ObservableCollection<int> { 11, 2, 33, 22 }, aansluitpin1 = new ObservableCollection<int> { 11, 2, 33, 22 }, stekker2 = new ObservableCollection<int> { 11, 2, 33, 22 }, aansluitpin2 = new ObservableCollection<int> { 11, 2, 33, 22 } });
            DataContext = store;
        }

我似乎无法使数据显示在我的数据网格中。

0 个答案:

没有答案