添加新项目时如何添加新的堆栈面板?

时间:2019-03-05 07:23:58

标签: c# wpf stackpanel

我面临的问题是我希望每次用户输入商品代码时,所有相关详细信息都会插入新的堆栈面板中。这意味着添加的每个项目都会添加一个新的堆栈面板。 1个stackpanel将有1个项目。但我不知道该怎么做。

这是用户输入条形码并按“ Enter”键后的代码。

private void txtItemCode_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {

            string itemCode = txtItemCode.Text;
            StackPanel spItemDisplay = new StackPanel();
            spItemDisplay.Orientation = Orientation.Horizontal;

            if (e.Key == Key.Return)
            {
              spItemDisplay.Children.Add(spItemDisplay);
                SimpleItem item = cashierViewModel.validateItemOnHandCode(txtItemCode.Text, 1);

                if (item != null)
                {
                        cashierViewModel.AddItemToList(item, PosWindows2.cashier.ShopId);
                        LoadData();
                        dgItemDisplay.ItemsSource = null;
                        dgItemDisplay.ItemsSource = CashierViewModel.itemDisplayList;
                }
                else
                {
                    MessageBox.Show("Item Not Available.", "Alert", MessageBoxButton.OK, MessageBoxImage.Information);
                }

                txtItemCode.Text = null;
            }
        }

这是.xaml

 <StackPanel x:Name="spItemDisplay" >
                     <ScrollViewer HorizontalAlignment="Right" >
                        <DataGrid CellEditEnding="DgItemDisplay_CellEditEnding" HorizontalAlignment="Center"  Width="1036" Name="dgItemDisplay" AutoGenerateColumns="False" Height="auto" >
                            <DataGrid.Columns>
                                <DataGridTextColumn IsReadOnly="True" x:Name="dgItemCode" Width="200" Header="Barcode" Binding="{Binding ItemCode}" />
                                <DataGridTextColumn IsReadOnly="True" x:Name="dgItemName" Width="350" Header="Item Name" Binding="{Binding ItemName}" />
                                <DataGridTextColumn IsReadOnly="True" x:Name="dgItemPrice" Width="150" Header="Item Price" Binding="{Binding ItemPrice}" />
                                <DataGridTextColumn x:Name="dgQuantity" Width="150" Header="Quantity" Binding="{Binding Quantity, UpdateSourceTrigger=PropertyChanged}" />
                                <DataGridTextColumn x:Name="dgDiscount" Width="150" Header="Discount" Binding="{Binding Discount, UpdateSourceTrigger=PropertyChanged}" />
                            </DataGrid.Columns>
                            <DataGrid.RowDetailsTemplate>
                                <DataTemplate  >
                                    <StackPanel Name="spItem" HorizontalAlignment="Center" >
                                        <Grid Margin="0,10"  >
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="300" />
                                                <ColumnDefinition Width="300" />
                                                <ColumnDefinition Width="100" />
                                                <ColumnDefinition Width="100" />
                                                <ColumnDefinition Width="*" />
                                            </Grid.ColumnDefinitions>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto" />
                                                <RowDefinition Height="Auto" />
                                            </Grid.RowDefinitions>
                                            <TextBlock Text="Quantity: " FontWeight="Bold" Grid.Column="2" Grid.Row="0"/>
                                            <TextBox x:Name="txtQty" Text="{Binding Quantity, UpdateSourceTrigger=PropertyChanged}" Grid.Column="3" Grid.Row="0"/>
                                            <TextBlock Text="Discount: " FontWeight="Bold" Grid.Column="2" Grid.Row="1"/>
                                            <TextBox x:Name="txtDisc" Text="{Binding Discount, UpdateSourceTrigger=PropertyChanged}" Grid.Column="3" Grid.Row="1"/>
                                        </Grid>
                                    </StackPanel>
                                </DataTemplate>
                            </DataGrid.RowDetailsTemplate>
                        </DataGrid>
                    </ScrollViewer>
                </StackPanel>

有人可以帮忙吗?我真的需要你的帮助。谢谢:)

1 个答案:

答案 0 :(得分:0)

您不能去拥有一个StackStack(用于spItemDisplay)作为ItemsPanelTemplate并指定DataTemplate作为您的StackPanel(spItem)的ItemsControl / ListView吗?

遵循以下原则:

    <ItemsControl ItemsSource="{Binding DisplayItems}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel x:Name="spItem">
                    <Grid Margin="0,10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>

                        <TextBlock Text="Quantity: "/>
                        <TextBlock Text="{Binding Quantity}"/>
                        <TextBlock/>
                    </Grid>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

更新:

如果您不熟悉DataTemplates,请查看注释中Andy的链接,或查看以下示例。

Items Control + DataTemplate explained