C# 只向一组datagrid列添加行,其他列应保持不变

C# 只向一组datagrid列添加行,其他列应保持不变,c#,wpf,xaml,wpfdatagrid,C#,Wpf,Xaml,Wpfdatagrid,我是WPF的新手,下面是我当前的XAML实现: <Window x:Class="TestingWPFPage.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expressi

我是WPF的新手,下面是我当前的XAML实现:

<Window x:Class="TestingWPFPage.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
    xmlns:local="clr-namespace:TestingWPFPage"
    mc:Ignorable="d"
    Title="MainWindow" Height="400" Width="600">
<Window.Resources>
    <col:ArrayList x:Key="arrList">
        <col:DictionaryEntry Key="Fixed" Value="1"/>
        <col:DictionaryEntry Key="Variable" Value="2"/>
    </col:ArrayList>        
</Window.Resources>
<Border Padding="2">
    <Grid>
        <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto">
            <DataGrid Margin="5,5,5,40" AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" VerticalAlignment="Stretch" Height="400" Name="dataGrid1" ItemsSource="{Binding GridCollection}">
                <DataGrid.Columns>

                    <!-- 1st Column - Select All Checkbox -->
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.Header>
                            <CheckBox Content="Select All" x:Name="headerCheckBox" />
                        </DataGridTemplateColumn.Header>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox Name="chkSelectAll" VerticalAlignment="Center"  HorizontalAlignment="Center" IsChecked="{Binding IsChecked, ElementName=headerCheckBox, Mode=OneWay}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <!-- 2nd Column - Consist - Select Fixed or Variable -->
                    <DataGridComboBoxColumn Header="Consist" Width="*"  SelectedValueBinding="{Binding Active}" ItemsSource="{StaticResource arrList}" DisplayMemberPath="Key" SelectedValuePath="Value"/>

                    <!-- 3rd Column - Vehicle Count -->
                    <DataGridTextColumn Header="Vehicle Count" Width="*" Binding="{Binding VehicleCount}"/>


                    <!-- 4th Column - Vehicles List -->
                    <DataGridTemplateColumn Header="Vehicles" Width="*">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel x:Name ="vehicleStackPanel" Orientation="Horizontal" Margin="3"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <!-- 5th Column - VATCs Selection Checkbox -->
                    <DataGridTemplateColumn Header="VATCs" Width="*">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" Margin="3">
                                    <CheckBox x:Name="selectVATCA" IsChecked="False" Margin="3"/>
                                    <Label Content="A" Margin="3"/>
                                    <CheckBox x:Name="selectVATCB" IsChecked="False" Margin="3"/>
                                    <Label Content="B" Margin="3"/>
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <!-- 6th Column - Region -->
                    <DataGridTextColumn Header="Region" Width="0.8*" Binding="{Binding Region}"/>

                    <!-- 7th Column - Segment -->
                    <DataGridTextColumn Header="Segment" Width="0.8*" Binding="{Binding Segment}"/>

                </DataGrid.Columns>
            </DataGrid>
        </ScrollViewer>
        <Button Height="20" Width="60" HorizontalAlignment="Left" Name="InitTrain" Click="InitTrain_Click" VerticalAlignment="Bottom" Margin="38,0,0,10">Initialize</Button>            
    </Grid>
</Border>

初始化

我正在寻找这样一个实现,堆栈面板应该添加到车辆列中

我试图从xaml到代码文件访问控件引用,但它找不到引用。 下面是我的代码

   public partial class MainWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    int vehicleCount = 0;
    int region = 0;
    int segment = 0;
    TextBox txtbox = new TextBox();

    // Consist Combobox Items
    public enum Consist { Fixed, Variable };


    public int VehicleCount
    {
        get { return vehicleCount; }
        set
        {
            vehicleCount = value;
            OnPropertyChanged("VehicleCount");

        }
    }

    public int Region
    {
        get { return region; }
        set
        {
            region = value;
            OnPropertyChanged("Region");
        }
    }
    public int Segment
    {
        get { return segment; }
        set
        {
            segment = value;
            OnPropertyChanged("Segment");
        }
    }

    public Consist ConsistList { get; set; }
    public string Vehicles { get; set; }
    public string VATCs { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        for (int i = 1; i <= vehicleCount; i++)
        {
            txtbox.Text = "textbox" + Convert.ToString(i);
            this.vehicleStackPanel.Childern.Add(txtbox);
        }

    }

    private void InitTrain_Click(object sender, RoutedEventArgs e)
    {

    }

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

}
public分部类主窗口:窗口,INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
int vehicleCount=0;
int区域=0;
int段=0;
TextBox txtbox=新建TextBox();
//组成组合框项目
公共枚举由{固定,变量}组成;
公共国际车辆计数
{
获取{返回车辆计数;}
设置
{
车辆计数=数值;
不动产变更(“车辆计数”);
}
}
公共区域
{
获取{返回区域;}
设置
{
区域=值;
不动产变更(“区域”);
}
}
公共整数段
{
获取{返回段;}
设置
{
段=值;
不动产变更(“分部”);
}
}
公共组一致性列表{get;set;}
公共字符串车辆{get;set;}
公共字符串VATCs{get;set;}
公共主窗口()
{
初始化组件();

对于(int i=1;我猜您误解了
DataGrid
的概念。在
DataGrid
中,不同列不可能有不同的行数。如果您需要将这3列作为一行,请将它们从
DataGrid
中提取出来,并放入
StackPanel
或其他容器中。您是否在考虑linmaster->detail的es,其中master行有多个要添加到的详细行?例如,您有一个名为Trucks的行和一个包含福特F-150、雪佛兰Silverado等的详细网格。@dymanoid:我尝试使用stackpanel实现逻辑,我编辑了新代码和xamls,但我现在无法在代码中引用它。