2

我想在 DataGrid 上绘制折线图。这将在我的网格的第一列,并且图表很大并且跨越了数据网格上的所有行。

我将如何处理这种情况?我应该使用画布吗?如果是这样,我应该为每个 DataGridCell 放置一个小 Canvas,还是可以以某种方式在 DataGrid 顶部渲染一个大 Canvas?

4

1 回答 1

2

好吧,您可以按照您在问题中所说的去做,即在网格顶部覆盖另一个控件,等等。但是与滚动同步,列/行调整大小可能被证明是一场噩梦。根据我的经验,已经多次这样做了,其中一种更简单的方法(从长远来看,即使一开始看起来更难)是提取 DataGrid 模板(使用混合)并根据您的需要对其进行修改。

提取 DataGrid 的模板后,您会发现:

....
<ControlTemplate TargetType="{x:Type DataGrid}">
    <Border ...>
       <ScrollViewer Focusable="false" Name="DG_ScrollViewer" Background="{TemplateBinding Background}">
          <ScrollViewer.Template>
             <ControlTemplate TargetType="{x:Type ScrollViewer}">
                  <Grid x:Name="DG_MainGrid" Background="{TemplateBinding Background}">

那是放置“SelectAllButton”、滚动条等的网格。您可以在其中放置任何内容,使其与您获得的任何列对齐:

您可以放置​​一个边框并放置图形(其中包含图表控件),使用 Binding 控制边框的宽度、高度、边距等,如下所示:

<Border Grid.Row="2" Grid.ColumnSpan="2" BorderThickness="2"    
        Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Views:MyView}}, Path=ChartBorderWidth}"
        Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Views:MyView}}, Path=ChartBorderHeight}"
        Margin="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Views:MyView}}, Path=ChartBorderMargin}">
        <..your charting control>
</Border>

无论如何,这只是一个建议,当您开始在 WPF 中做一些自定义的事情并且不仅仅是开箱即用的解决方案时,事情太复杂了,无法给您确切的答案。但我希望这会有所帮助..

于 2012-04-20T15:18:57.043 回答