首页 > 解决方案 > 仅将行添加到一组数据网格列,其他列应保持不变

问题描述

我是 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>

我正在寻找这样的实现,应将堆栈面板添加到列Vehicles中。

我正在尝试从 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));
    }

}

}

有没有办法在 XAML 中完成这项工作,或者我们是否需要为此编写代码,如果是这样 - 我该如何进行?

标签: c#wpfxamlwpfdatagrid

解决方案


推荐阅读