上面的问题可能有点混乱,所以我在这里解释。
当前我在WPF中使用Expander
下面是我的代码
<materialDesign:Card Background="{DynamicResource MaterialDesignBackground}">
<StackPanel x:Name="spItemDisplay" DataContext="{Binding itemDisplayList}">
<Expander x:Name="expander1" HorizontalAlignment="Stretch" Header="{Binding ItemName}">
<StackPanel Orientation="Vertical" TextBlock.Foreground="{DynamicResource MaterialDesignBody}" Margin="24,8,24,16">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label FontWeight="Bold" Content="Item Code" Grid.Column="0" Grid.Row="0" />
<TextBlock Text="{Binding ItemCode}" Grid.Column="1" Grid.Row="0"/>
<Label FontWeight="Bold" Content="Item Name" Grid.Column="0" Grid.Row="1" />
<TextBlock Text="{Binding ItemName}" Grid.Column="1" Grid.Row="1"/>
<Button Click="btnRemoveItem_Click" Grid.Row="9" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Width="30" Height="30" Padding="0" Background="{x:Null}" BorderBrush="{x:Null}">
<materialDesign:PackIcon Background="Transparent" Foreground="#FF3580BF" Kind="RemoveShoppingCart" Width=" 30" Height="30"/>
</Button>
</Grid>
</StackPanel>
</Expander>
</StackPanel>
</materialDesign:Card>
我的界面看起来像这样
基本上,此接口的工作方式如下: 当用户插入条形码时,商品代码将显示在扩展器中。它从后面的代码绑定中获取详细信息。
当前,该扩展器工作正常。但我只能添加1个项目。当我添加第二个项目时,它没有出现在扩展器中。
我想要的是,当用户添加第二个项目时,扩展器将增加到2个。
是否有可能做到这一点?
itemDisplayList
来自此代码
cashierViewModel.AddItemToList(item);
spItemDisplay.DataContext = null;
spItemDisplay.DataContext = CashierViewModel.itemDisplayList;
我之前已经使用DataGrid完成了它的工作,但是我希望它显示在诸如Expander之类的东西中
基本上itemDisplayList
包含所有添加的项目。
答案 0 :(得分:2)
StackPanel是在此处使用错误的容器。您需要使用一个可以显示多个绑定数据项的容器。 ItemsControl是一个不错的选择,但请注意,它对数据使用ItemsSource,并且您需要将DataContext设置在该级别之上。
<materialDesign:Card Background="{DynamicResource MaterialDesignBackground}">
<ItemsControl x:Name="spItemDisplay" ItemsSource="{Binding itemDisplayList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander x:Name="expander1" HorizontalAlignment="Stretch" Header="{Binding ItemName}">
<StackPanel Orientation="Vertical" TextBlock.Foreground="{DynamicResource MaterialDesignBody}" Margin="24,8,24,16">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label FontWeight="Bold" Content="Item Code" Grid.Column="0" Grid.Row="0" />
<TextBlock Text="{Binding ItemCode}" Grid.Column="1" Grid.Row="0"/>
<Label FontWeight="Bold" Content="Item Name" Grid.Column="0" Grid.Row="1" />
<TextBlock Text="{Binding ItemName}" Grid.Column="1" Grid.Row="1"/>
<Button Click="btnRemoveItem_Click" Grid.Row="9" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Width="30" Height="30" Padding="0" Background="{x:Null}" BorderBrush="{x:Null}">
<materialDesign:PackIcon Background="Transparent" Foreground="#FF3580BF" Kind="RemoveShoppingCart" Width=" 30" Height="30"/>
</Button>
</Grid>
</StackPanel>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</materialDesign:Card>