0

我想在 Windows 8 Metro 应用程序中显示 8x8 网格。去做这个:

  1. 我创建了一个Grid,并添加了 8 个行定义和 8 个列定义。
  2. 然后我为每个网格单元添加一个Rectangle带有黑色边框的。
  3. 然后在MeasureOverride方法中,我检查了availableSize. 由于我的网格需要是正方形的(纵横比 = 1.0),我计算 的最小值availableSize.Width, availableSize.Height并返回一个等于 的新大小(minimum, minimum)

但是,这不起作用。结果网格的大小等于availableSize,而不是我从MeasureOverride方法返回的大小。如果我修改MeaureOverride, 以便将s 设置为Height,并将s 设置为,那么它可以工作。但我看过一些视频,他们说你不应该明确设置任何东西的属性。RowDefinitionminimumWidthColumnDefinitionminimumHeightWidth

那么,有没有更好的方法来完成我想要的?

4

2 回答 2

0

一种解决方案是创建一个自定义 Grid 控件来处理宽度、高度

    public class SquareGrid : Grid
{
    public SquareGrid()
    {
        this.SizeChanged += OnSizeChanged;
        this.Loaded += OnLoaded;
    }

    private void OnSizeChanged(object sender, SizeChangedEventArgs e)
    {
        var parent = VisualTreeHelper.GetParent(this) as FrameworkElement;
        if (parent == null) return;

        ResizeToSquare(parent);
    }

    private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        var parent = VisualTreeHelper.GetParent(this) as FrameworkElement;
        if (parent == null) return;

        parent.SizeChanged += ParentOnSizeChanged;
    }

    private void ParentOnSizeChanged(object sender, SizeChangedEventArgs sizeChangedEventArgs)
    {
        FrameworkElement parent = sender as FrameworkElement;
        if (parent == null) return;

        ResizeToSquare(parent);
    }

    private void ResizeToSquare(FrameworkElement parent)
    {
        var min = Math.Min(parent.ActualHeight, parent.ActualWidth);

        this.Width = min;
        this.Height = min;
    }
}

你也可以为此建立一个行为来做同样的事情。

于 2012-06-15T21:57:52.577 回答
0

我不确定您是否需要以任何方式与这些单元格进行交互,但如果您只想绘制一个网格,这里有一个快速控制。它将填充父控件的空间。

public class GridShape : Control
{
    public int Columns
    {
        get { return (int)GetValue(ColumnsProperty); }
        set { SetValue(ColumnsProperty, value); }
    }
    public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register("Columns", typeof(int), typeof(GridShape), new PropertyMetadata(8));

    public int Rows
    {
        get { return (int)GetValue(RowsProperty); }
        set { SetValue(RowsProperty, value); }
    }
    public static readonly DependencyProperty RowsProperty = DependencyProperty.Register("Rows", typeof(int), typeof(GridShape), new PropertyMetadata(8));

    public Brush Stroke
    {
        get { return (Brush)GetValue(StrokeProperty); }
        set { SetValue(StrokeProperty, value); }
    }
    public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register("Stroke", typeof(Brush), typeof(GridShape), new PropertyMetadata(new SolidColorBrush(Colors.Black)));

    public double StrokeThickness
    {
        get { return (double)GetValue(StrokeThicknessProperty); }
        set { SetValue(StrokeThicknessProperty, value); }
    }
    public static readonly DependencyProperty StrokeThicknessProperty = DependencyProperty.Register("StrokeThickness", typeof(double), typeof(GridShape), new PropertyMetadata(1.0));

    protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
    {
        Pen pen = new Pen(Stroke, StrokeThickness);

        double heightSpan = ActualHeight / Rows;
        double widthSpan = ActualWidth / Columns;

        for (double y = 0; y <= ActualHeight; y += heightSpan)
            drawingContext.DrawLine(pen, new Point(0, y), new Point(ActualWidth, y));

        for (double x = 0; x <= ActualWidth; x += widthSpan)
            drawingContext.DrawLine(pen, new Point(x, 0), new Point(x, ActualHeight));
    }
}
于 2012-06-15T17:53:51.887 回答