添加新项目时如何添加新的堆栈面板?
How to add new stackpanel when new item added?
我面临的问题是我希望每次用户输入项目代码时,所有相关详细信息都插入到新的堆栈面板中。这意味着添加的每个项目都会添加一个新的堆栈面板。 1 个堆栈面板将有 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>
有人可以帮忙吗?我真的需要你的帮助。谢谢:)
你不能选择一个 ItemsControl/ListView,它有一个 StackPanel(用于 spItemDisplay)作为 ItemsPanelTemplate 并将 DataTemplate 指定为你的 StackPanel(spItem)吗?
大致如下:
<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 的 link 或查看以下示例。
我面临的问题是我希望每次用户输入项目代码时,所有相关详细信息都插入到新的堆栈面板中。这意味着添加的每个项目都会添加一个新的堆栈面板。 1 个堆栈面板将有 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>
有人可以帮忙吗?我真的需要你的帮助。谢谢:)
你不能选择一个 ItemsControl/ListView,它有一个 StackPanel(用于 spItemDisplay)作为 ItemsPanelTemplate 并将 DataTemplate 指定为你的 StackPanel(spItem)吗?
大致如下:
<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 的 link 或查看以下示例。