简体   繁体   English

如何使用XAML在WPF应用程序中将userControl放在其下方的同时使数据网格可滚动?

[英]How to make a data-grid scrollable while placing a userControl under it in WPF app using XAML?

I have a WPF application. 我有一个WPF应用程序。 I am rendering the data into a data-grid. 我正在将数据渲染到数据网格中。 I want the DataGrid to expand as need to take the full height of the screen. 我希望DataGrid能够根据需要扩展到整个屏幕高度。 If there are more data than the height of the screen, I want to automatically show scrollbar. 如果数据多于屏幕高度,我想自动显示滚动条。

Also, below the data-grid, I have a ContentControl section which renders a pagination for the data. 另外,在数据网格下方,我有一个ContentControl部分,该部分呈现数据的分页。 How can I place these two under each other at the same time while showing scroll bar if needed around the data-grid. 如果需要围绕数据网格,如何在显示滚动条的同时将这两个对象放在一起。

Here is my XAML code 这是我的XAML代码

<Grid Style="{StaticResource ContentRoot}">
    <StackPanel>

        <DataGrid ItemsSource="{Binding Vendors}"
                  SelectedItem="{Binding SelectedVendor}"
                  AutoGenerateColumns="False"
                  HorizontalAlignment="Stretch"
                  VerticalAlignment="Center"
                  CanUserAddRows="False"
                  VerticalContentAlignment="Center"
                  ScrollViewer.CanContentScroll="True"
                  ScrollViewer.VerticalScrollBarVisibility="Auto"
                  ScrollViewer.HorizontalScrollBarVisibility="Auto">

            <DataGrid.Columns>

            <DataGridTextColumn Header="Name"
                                Binding="{Binding Name}"
                                />
                <DataGridTextColumn Header="Account Number"
                                    Binding="{Binding AccountCode}" />

                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button VerticalAlignment="Center"
                                    Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
                                    Path=DataContext.ShowVendor}"
                                    CommandParameter="{Binding}">
                                <StackPanel Orientation="Horizontal">

                                    <fa:FontAwesome Icon="Eye"
                                                    FontSize="18" />
                                    <Label Content="Details" 
                                           Padding="7 0 0 0" />
                                </StackPanel>
                            </Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button VerticalAlignment="Center"
                                    Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
                                                      Path=DataContext.DeleteVendor}"
                                    CommandParameter="{Binding}">
                                <StackPanel Orientation="Horizontal">

                                    <fa:FontAwesome Icon="Trash"
                                                    FontSize="18" />
                                    <Label Content="Delete"
                                           Padding="7 0 0 0" />
                                </StackPanel>
                            </Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

            </DataGrid.Columns>

        </DataGrid>


        <ContentControl Content="{Binding PageMeta}"></ContentControl>

    </StackPanel>

</Grid>

I also tried to add DockPanel and wrap my datagrid with ScrollViewer but that did not work either. 我还尝试添加DockPanel并用ScrollViewer包装我的数据网格,但这也不起作用。 The scroll bar works but the PageMeta Content Control is not visible. 滚动条可以使用,但是PageMeta Content Control不可见。

Here is how I added the ScrollViewer 这是我添加ScrollViewer的方法

<Grid Style="{StaticResource ContentRoot}">

    <DockPanel>
        <ScrollViewer DockPanel.Dock="Top">
        <StackPanel >

                <DataGrid ItemsSource="{Binding Vendors}"
                          SelectedItem="{Binding SelectedVendor}"
                          AutoGenerateColumns="False"
                          HorizontalAlignment="Stretch"
                          VerticalAlignment="Center"
                          CanUserAddRows="False"
                          VerticalContentAlignment="Center">

                    <DataGrid.Columns>

                        <DataGridTextColumn Header="Name"
                                            Binding="{Binding Name}" />
                        <DataGridTextColumn Header="Account Number"
                                            Binding="{Binding AccountCode}" />

                        <DataGridTemplateColumn>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Button VerticalAlignment="Center"
                                            Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
                                            Path=DataContext.ShowVendor}"
                                            CommandParameter="{Binding}">
                                        <StackPanel Orientation="Horizontal">

                                            <fa:FontAwesome Icon="Eye"
                                                            FontSize="18" />
                                            <Label Content="Details"
                                                   Padding="7 0 0 0" />
                                        </StackPanel>
                                    </Button>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>

                        <DataGridTemplateColumn>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Button VerticalAlignment="Center"
                                            Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
                                                              Path=DataContext.DeleteVendor}"
                                            CommandParameter="{Binding}">
                                        <StackPanel Orientation="Horizontal">

                                            <fa:FontAwesome Icon="Trash"
                                                            FontSize="18" />
                                            <Label Content="Delete"
                                                   Padding="7 0 0 0" />
                                        </StackPanel>
                                    </Button>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>

                    </DataGrid.Columns>

                </DataGrid>


        </StackPanel>
        </ScrollViewer>
        <StackPanel DockPanel.Dock="Bottom">
            <ContentControl Content="{Binding PageMeta}"></ContentControl>

        </StackPanel>

    </DockPanel>

</Grid>

This did the trick 这成功了

<DockPanel Style="{StaticResource ContentRoot}"
           Margin="0"
           LastChildFill="True"
           VerticalAlignment="Top">

    <StackPanel DockPanel.Dock="Bottom" Margin="0 10 0 0">
        <ContentControl Content="{Binding PageMeta}"></ContentControl>
    </StackPanel>


    <ScrollViewer VerticalScrollBarVisibility="Auto" VerticalAlignment="Top">

            <DataGrid ItemsSource="{Binding Vendors}"
                        SelectedItem="{Binding SelectedVendor}"
                        AutoGenerateColumns="False"
                        HorizontalAlignment="Stretch"
                        VerticalAlignment="Center"
                        CanUserAddRows="False"
                        VerticalContentAlignment="Center">

            <DataGrid.Columns>

                <DataGridTextColumn Header="Name"
                                    Binding="{Binding Name}" />
                <DataGridTextColumn Header="Account Number"
                                    Binding="{Binding AccountCode}" />

                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button VerticalAlignment="Center"
                                    Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
                                        Path=DataContext.ShowVendor}"
                                    CommandParameter="{Binding}">
                                <StackPanel Orientation="Horizontal">

                                    <fa:FontAwesome Icon="Eye"
                                                    FontSize="18" />
                                    <Label Content="Details"
                                           Padding="7 0 0 0" />
                                </StackPanel>
                            </Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button VerticalAlignment="Center"
                                    Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
                                                            Path=DataContext.DeleteVendor}"
                                    CommandParameter="{Binding}">
                                <StackPanel Orientation="Horizontal">

                                    <fa:FontAwesome Icon="Trash"
                                                    FontSize="18" />
                                    <Label Content="Delete"
                                           Padding="7 0 0 0" />
                                </StackPanel>
                            </Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

            </DataGrid.Columns>
        </DataGrid>


    </ScrollViewer>



</DockPanel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM