WPF 画布和网格覆盖

use*_*851 4 wpf grid overlay canvas

我有一个Grid代表一些数据的对象,我需要一个Canvas覆盖在它上面来布局一些行。它Canvas在它自己的里面UserControl

问题是,Canvas当调整宽度和高度大小时, 及其内容应该自动调整大小Grid

Canvas在里面添加了 a ViewBox,但它没有起作用。当Grid调整大小时,则Canvas不会。其目的Canvas是在网格顶部覆盖类似标尺的功能。

期待您的解决方案。

编辑

我无法使用网格上的样式来替换画布,因为网格显示的信息与画布不同。将其视为图表,其中有不同大小的条形列(在我的例子中是网格),而日期是叠加中的线条(就像甘特图一样)

我的代码:

    taxCanvas = new TimeAxis();
    Grid.SetRowSpan(taxCanvas, GRightMain.RowDefinitions.Count);
    Grid.SetColumnSpan(taxCanvas, GRightMain.ColumnDefinitions.Count);

    Grid.SetColumn(taxCanvas, 0);
    Grid.SetRow(taxCanvas, 0);


    Grid.SetZIndex(taxCanvas, -1);

    taxCanvas.Height = GRight.ActualHeight;
    taxCanvas.Width = GRight.ActualWidth;

    GRightMain.Children.Add(taxCanvas);
Run Code Online (Sandbox Code Playgroud)

TimeAxis 是我的画布用户控件,GRightMain 是一个网格,它包含我的画布和内容(Gright)位于同一行和列中的网格。

希望这可以帮助

tro*_*ous 5

在我看来,画布绝对是错误的方法。

我强烈建议查找装饰器。您可以创建一个自定义装饰器来完成此操作。

Adorner 基本上是一个位于所有 UIElement 之上的“非交互式窗口”。它允许您执行任何您想要的操作(创建控件、绘制内容等),这些操作将显示在控件本身的顶部。

想象一张木制咖啡桌,上面有一块透明玻璃。如果你在透明玻璃上画画,你仍然可以看到咖啡桌。唯一的区别是,您实际上可以直接穿过咖啡桌上的透明玻璃并触摸木材本身。

我讨厌发布 MSDN 链接...但是...呃。在这种情况下,这将是一个好的开始:

http://msdn.microsoft.com/en-us/library/ms743737.aspx

编辑:

我很快就把一些东西放在一起。希望这有帮助吗?

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:loc="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
  <Grid>
     <loc:GridWithRulerxaml></loc:GridWithRulerxaml>
     <Button Height="20" Width="50" >Click me</Button>
     <TextBox Width="150" Height="25" HorizontalAlignment="Left">This is a text box</TextBox>
  </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

用户控制:

<UserControl x:Class="WpfApplication1.GridWithRulerxaml"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
  <Grid>

  </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

用户控制隐藏代码:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace WpfApplication1
{
  /// <summary>
  /// Interaction logic for GridWithRulerxaml.xaml
  /// </summary>
  public partial class GridWithRulerxaml : UserControl
  {
    public GridWithRulerxaml()
    {
      InitializeComponent();

      //Loaded event is necessary as Adorner is null until control is shown.
      Loaded += GridWithRulerxaml_Loaded;

    }

    void GridWithRulerxaml_Loaded(object sender, RoutedEventArgs e)
    {
      var adornerLayer = AdornerLayer.GetAdornerLayer(this);
      var rulerAdorner = new RulerAdorner(this);
      adornerLayer.Add(rulerAdorner);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

最后是装饰者本身:

using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace WpfApplication1
{
  public class RulerAdorner : Adorner
  {
    private FrameworkElement element;
    public RulerAdorner(UIElement el) : base(el)
    {
      element = el as FrameworkElement;
    }

    protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
    {
      base.OnRender(drawingContext);

      double height = element.ActualHeight;
      double width = element.ActualWidth;

      double linesHorizontal = height/50;
      double linesVertical = width/50;

      var pen = new Pen(Brushes.RoyalBlue, 2) { StartLineCap = PenLineCap.Triangle, EndLineCap = PenLineCap.Triangle };

      int offset = 0;

      for (int i = 0; i <= linesVertical; ++i)
      {
        offset = offset + 50;
        drawingContext.DrawLine(pen, new Point(offset, 0), new Point(offset, height));
      }

      offset = 0;

      for (int i = 0; i <= linesHorizontal; ++i)
      {
        offset = offset + 50;
        drawingContext.DrawLine(pen, new Point(0, offset), new Point(width, offset));
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

如果您希望我详细说明代码本身,请告诉我。

我确认这会在主页上的任何内容之上绘制一个网格。您应该仍然能够与下面的内容进行交互。