【问题标题】:How do I include a custom row at the end of a DataGrid in Silverlight?如何在 Silverlight 中的 DataGrid 末尾包含自定义行?
【发布时间】:2010-10-05 11:41:32
【问题描述】:

我的 Silverlight 应用程序中有一个 DataGrid,它运行良好,在我操作 ItemsSource 集合时添加一行或删除一行。但是,我希望有一个额外的行,或始终出现在最后一个数据行之后的控件。

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

有没有一种方法可以让 RowsPresenter 上的星形高度行为,同时仍然让我的控件以我想要的方式显示?

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

想法?

提前感谢您的帮助。

更新

我还问了一个关于将一个控件固定在另一个控件下方的问题(并最终得到了回答),如果您不希望自定义行与其余行一起滚动(例如在我的情况下,我希望另一个数据网格标题行显示总计并浮动在其他行上)。

How do I pin one control below another in Silverlight?

【问题讨论】:

    标签: c# xaml datagrid silverlight-2.0 controltemplates


    【解决方案1】:

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

    首先,我将我的自定义行控件和 RowsPresenter 组合在一个由两行组成的网格中,每行的大小为 Auto。然后我将网格放置在 ScrollViewer 中,然后将滚动查看器行的大小调整为星形大小。我没有将 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>
    

    【讨论】:

    • +1 你能写一个使用你的模板的例子吗?谢谢!
    【解决方案2】:

    不确定这是否对 Silverlight 有帮助,但我通过添加名为 IsTotal 的不可见列向 WPF DataGrid 添加了总计行。我能够使用自定义分组/排序让这一行始终出现在网格的底部。分组/排序顺序被配置为将此列用作主要排序,具有固定方向。看起来效果不错。

    【讨论】:

    • 对于一行数据来说这是一个很酷的解决方案。我喜欢。它不适合我的需要,但它仍然是一种有趣的方法。谢谢。
    【解决方案3】:

    首先,为 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>
    

    除了根 Grid 之外,您可能还有任何其他垂直延伸的容器,重要的是它会尝试为其填充所有可用空间。

    【讨论】:

      猜你喜欢
      • 2018-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-26
      相关资源
      最近更新 更多