繁体   English   中英

如何在Silverlight中的DataGrid末尾包含自定义行?

[英]How do I include a custom row at the end of a DataGrid in Silverlight?

我在Silverlight应用程序中有一个DataGrid ,它可以很好地工作,在我操作ItemsSource集合时添加一行或删除一行。 但是,我希望在最后一个数据行之后有一个额外的行或控件。

我可以使用ControlTemplate在最后一行之后显示附加ControlTemplate并将RowsPresenter行设置为自动高度,但这意味着当渲染区域变得太小时,行永远不会滚动。 但是,如果我将RowsPresenter行高度更改为Star,则行会滚动,但附加控件会显示为固定到数据网格的底部而不是最后一行的底部。

有没有办法在RowsPresenter上拥有Star高度行为,同时仍然按照我想要的方式显示我的控件?

我目前的想法是,我需要以某种方式使用LoadingRow事件来查找最后一行的位置,并使用Canvas或类似方法将我的控件放在适当的位置。

思考?

在此先感谢您的帮助。

更新

我还问了一个问题(并最终回答)关于将一个控件固定在另一个控件下面的问题,如果您不希望自定义行与其余行一起滚动(例如在我的情况下,那么可以用来解决此问题)我希望另一个datagrid标题行显示总计并浮动其他行)。

如何在Silverlight中将一个控件固定在另一个控件下面?

昨晚我在一阵灵感中解决了我的问题。 我注意到没有其他人投票赞成这个问题,所以这个答案对任何人都没有帮助,但以防万一。

首先,我将自定义行控件和RowsPresenter合并为两行网格,每行大小为Auto。 然后我将网格放在ScrollViewer中,然后将滚动查看器行的大小调整为Star大小。 我没有将VerticalScrollbar模板部件添加到我的模板中,因为这只会滚动RowsPresenter。

这给了我确切的行为,我正在寻找添加行的位置,自定义行仍然固定在最后一个数据行的底部。 当行和自定义行溢出可见区域的末尾时,滚动条似乎允许滚动,同时保持标题固定到位。

任务完成。 我希望有人觉得这很有帮助。 下面是我的ControlTemplate XAML。

<ControlTemplate TargetType="swcd:DataGrid" x:Key="DataGridTemplate">
    <Border
        BorderBrush="{TemplateBinding BorderBrush}"
        BorderThickness="{TemplateBinding BorderThickness}">

        <Grid Name="Root" Background="{TemplateBinding Background}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <swcdp:DataGridColumnHeader Name="TopLeftCornerHeader" Grid.Column="0"/>
            <swcdp:DataGridColumnHeadersPresenter Name="ColumnHeadersPresenter" Grid.Column="1"/>
            <swcdp:DataGridColumnHeader Name="TopRightCornerHeader" Grid.Column="2"/>

            <ScrollViewer
                Grid.Row="1"
                Grid.Column="1"
                Grid.ColumnSpan="1"
                Padding="0,0,0,0"
                BorderThickness="0,0,0,0"
                VerticalScrollBarVisibility="Auto">
                <Grid >
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <swcdp:DataGridRowsPresenter Name="RowsPresenter" Grid.Row="0" />

                    <Border
                        Margin="1,1,1,1"
                        Padding="2,2,2,2"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Grid.Row="1">
                        <Grid Background="{TemplateBinding Background}">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>

                            <TextBlock
                                Grid.Row="0"
                                TextAlignment="Left"
                                TextWrapping="NoWrap"
                                Text="Add a new item using the lists below:" />

                            <mystuff:MySelectionControl
                                HorizontalContentAlignment="Stretch"
                                Grid.Row="1"
                                SelectionChanged="OnSelectionChanged"/>
                        </Grid>
                    </Border>
                </Grid>
            </ScrollViewer>

            <Rectangle Name="BottomLeftCorner" Grid.Row="3" Grid.ColumnSpan="2" />
            <Grid Grid.Column="1" Grid.Row="3">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Rectangle Name="FrozenColumnScrollBarSpacer" />
                <ScrollBar Name="HorizontalScrollbar" Grid.Column="1" Orientation="Horizontal" Height="18" />
            </Grid>
            <Rectangle Name="BottomRightCorner" Grid.Column="2" Grid.Row="3" />
        </Grid>
    </Border>
</ControlTemplate>

不确定这对Silverlight是否有帮助,但我通过添加名为IsTotal的不可见列向WPF DataGrid添加了一个总计行。 我能够使用自定义分组/排序将此行始终显示在网格的底部。 分组/排序顺序已配置为使用此列作为主要排序,并具有修订方向。 似乎运作良好。

首先,为DataGrid和固定控件创建一个Grid:

<Grid Grid.Row="0" VerticalAlignment="Top">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />               
        <RowDefinition Height="Auto" />            
    </Grid.RowDefinitions>

    <sdk:DataGrid Grid.Row="0" ItemsSource="{Binding YOUR_COLLECTION}" />

    <TextBlock Grid.Row="1" Text="Hello World" /> <!-- The pinned control. -->
</Grid>

技巧是VerticalAlignment =“Top” - 当DataGrid小于可用高度时,它将移动到可用空间的顶部,并且固定控件将显示在其下方。

然后,将此Grid放入一个垂直拉伸的容器中,例如放入另一个具有Star高度的Grid的行中:

<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <!-- RowDefition for the Grid with the DataGrid with the pinned control. --> 
        <!-- If you want to have some other controls, -->
        <!-- add other RowDefinitions and put these controls there.  -->
        <RowDefinition Height="*" /> 
    </Grid.RowDefinitions>

    <!-- The internal Grid for the DataGrid & the pinned control. -->
    <Grid Grid.Row="0" VerticalAlignment="Top">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />               
            <RowDefinition Height="Auto" />            
        </Grid.RowDefinitions>

        <sdk:DataGrid Grid.Row="0" ItemsSource="{Binding YOUR_COLLECTION}" />

        <TextBlock Grid.Row="1" Text="Hello World" /> <!-- The pinned control. -->
    </Grid>
</Grid>

您可以使用任何其他垂直延伸的容器来代替根网格,重要的是它会尝试为其填充所有可用空间。

暂无
暂无

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

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