列表视图中的WPF卡不会动态创建

时间:2018-10-02 12:07:21

标签: c# wpf xaml

我对WPF不熟悉,目前我正在使用WPF列表视图动态创建UI之类的卡片,到目前为止,xaml代码如下

<Grid x:Name="LayoutRoot" Background="Beige">        
        <ScrollViewer HorizontalScrollBarVisibility="Auto">
            <StackPanel VerticalAlignment="Top" HorizontalAlignment="Left">
                <Grid>       
                    <Grid.RowDefinitions>
                        <RowDefinition Height="319*"></RowDefinition>
                        <RowDefinition Height="4*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>

                    <ItemsControl>
                        <GridSplitter  Grid.Row="3" HorizontalAlignment="Stretch" Height="13" Background="Red" VerticalAlignment="Center"/>
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <UniformGrid Columns="2" />
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Button Content="{Binding}" Margin="0,0,5,5" />
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>

                    <ListView Grid.Column="1" Margin="10,10,10,6" VirtualizingStackPanel.IsVirtualizing="True"
                    VirtualizingStackPanel.VirtualizationMode="Recycling" x:Name="OrderDataGridItems"
                    HorizontalContentAlignment="Stretch" Background="{x:Null}"  BorderBrush="{x:Null}"
                    ScrollViewer.CanContentScroll="True"  
                    ScrollViewer.HorizontalScrollBarVisibility="Auto"
                    ScrollViewer.VerticalScrollBarVisibility="Hidden"
                    SelectionMode="Single"  IsSynchronizedWithCurrentItem="True" Grid.Row="0" 
                              >
                        <ListView.Resources>
                            <converters:CollectionViewGroupToFieldConverter x:Key="CollectionViewGroupToFieldConverter"/>

                            <ControlTemplate x:Key="GroupItemTemplate">
                                <Expander IsExpanded="False" VerticalAlignment="Bottom" VerticalContentAlignment="Bottom">

                                    <ItemsPresenter />
                                </Expander>
                            </ControlTemplate>

                            <Page x:Key="s">
                                <Page.Resources>

                                </Page.Resources>
                            </Page>
                        </ListView.Resources>

                        <ListView.GroupStyle>
                            <GroupStyle HidesIfEmpty="True">
                                <GroupStyle.HeaderTemplate>
                                    <DataTemplate>

                                        <DockPanel Height="100">
                                            <Grid DockPanel.Dock="Bottom" Height="50">
                                                <Grid.Resources>

                                                </Grid.Resources>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="*" />
                                                    <ColumnDefinition Width="*" />
                                                </Grid.ColumnDefinitions>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition />
                                                    <RowDefinition />
                                                </Grid.RowDefinitions>
                                                <StackPanel Orientation="Horizontal">
                                                    <TextBlock Grid.Column="0" Text="Total: " FontWeight="Bold"/>
                                                    <TextBlock Grid.Column="0" Text="{Binding Path=Name}" />
                                                </StackPanel>
                                                <TextBlock Grid.Row="1" Grid.Column="1" HorizontalAlignment="Right" Text="{Binding Name}" />

                                                <Button 
                                                Grid.Row="1"
                                                Grid.Column="0"
                                                HorizontalAlignment="Left"
                                                Width="100"
                                                Name="btnAdd"
                                                Content="Add Item"
                                                >

                                                </Button>
                                                <Line Grid.Column="1" Stroke="Black" X2="500" Fill="Black" VerticalAlignment="Center" />


                                            </Grid>
                                            <ItemsPresenter />
                                        </DockPanel>
                                    </DataTemplate>
                                </GroupStyle.HeaderTemplate>

                                <GroupStyle.ContainerStyle>
                                    <Style TargetType="{x:Type GroupItem}">

                                        <Setter Property="Template">
                                            <Setter.Value>
                                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                                    <Expander IsExpanded="True" BorderBrush="#FFA4B97F" BorderThickness="0,0,0,1">
                                                        <Expander.Header>
                                                            <StackPanel Orientation="Horizontal">
                                                                <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Gray" FontSize="22" Margin="5,0,0,0" Width="100" />
                                                                <TextBlock Text=" (" FontSize="22" Foreground="Gray" FontWeight="Bold"/>
                                                                <TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Gray" FontWeight="Bold"  />
                                                                <TextBlock Text=")" FontSize="22" Foreground="Gray" FontWeight="Bold"/>
                                                            </StackPanel>
                                                        </Expander.Header>
                                                        <Expander.Content>
                                                            <ItemsPresenter />
                                                        </Expander.Content>
                                                    </Expander>
                                                </ControlTemplate>
                                            </Setter.Value>
                                        </Setter>
                                    </Style>
                                </GroupStyle.ContainerStyle>
                            </GroupStyle>
                        </ListView.GroupStyle>
                        <ListView.ItemContainerStyle>
                            <Style TargetType="ListViewItem">
                                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                                <Setter Property="VerticalContentAlignment" Value="Stretch" />
                            </Style>
                        </ListView.ItemContainerStyle>
                        <ListView.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Vertical" />
                            </ItemsPanelTemplate>
                        </ListView.ItemsPanel>
                        <ListView.View>
                            <GridView>
                            <GridViewColumn Header="" Width="100">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text=""/>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn Header="Item" Width="120" DisplayMemberBinding="{Binding Description}"/>
                            <GridViewColumn Header="Item" Width="120" DisplayMemberBinding="{Binding OrderTime}"/>
                            <GridViewColumn Header="Item" Width="120" DisplayMemberBinding="{Binding QTY}"/>
                        </GridView>
                        </ListView.View>
                    </ListView>    
                </Grid>                
                </StackPanel>
        </ScrollViewer>      
    </Grid>

.cs代码后面

private void GetOrderDetail()
{
    try
    {                
                    DataTable dtKitchenOrders = transactionService.GetItemDetailsForKitchenOrder();

                    OrderDataGridItems.Items.Clear();                
                    OrderDataGridItems.ItemsSource = dtKitchenOrders.DefaultView; 
                    CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(OrderDataGridItems.ItemsSource);
                    PropertyGroupDescription groupDescription1 = new PropertyGroupDescription("TableNo");
                    PropertyGroupDescription groupDescription2 = new PropertyGroupDescription("OrderNo");
                    view.GroupDescriptions.Add(groupDescription1);
                    view.GroupDescriptions.Add(groupDescription2);               
    }
    catch (Exception ex)
    {

    }
} 

问题是上面的代码生成了我不期望的输出,如下所示(输出图像如下) enter image description here

我期望的输出如下

enter image description here

我对xaml感到困惑,我需要绑定什么集合才能实现预期的输出。非常感谢您的帮助,并在此先感谢您的支持(示例代码可能会有所帮助)

0 个答案:

没有答案