简体   繁体   English

WPF:在用户绘制的控件中显示二维数组的正确方法是什么

[英]WPF: what is the right way to display a 2D array in a user-drawn control

I want to make a user-drawn control for the only purpose of displaying a Color[,] array.我想制作一个用户绘制的控件,其唯一目的是显示一个 Color[,] 数组。 The control itself should draw an NxM grid of rectangles of corresponding colors.控件本身应该绘制一个 NxM 矩形网格,对应的 colors。

I'm trying to inherit from a FrameworkElement and to override OnRender method:我正在尝试从 FrameworkElement 继承并覆盖 OnRender 方法:

public class CustomControl1 : FrameworkElement
{
    static CustomControl1()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
    }

    public Color[,] ColorCollection
    {
        get { return (Color[,])GetValue(ColorGridProperty); }
        set { SetValue(ColorGridProperty, value); }
    }

    public static readonly DependencyProperty ColorGridProperty =
        DependencyProperty.Register("ColorCollection", typeof(Color[,]), typeof(CustomControl1), 
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));

    protected override void OnRender(DrawingContext drawingContext)
    {
        if (ColorCollection != null)
        {
            int dimx = this.ColorCollection.GetLength(0);
            int dimy = this.ColorCollection.GetLength(1);

            double w = this.ActualWidth / dimx;
            double h = this.ActualWidth / dimy;

            for (int x = 0; x < dimx; x++)
            {
                for (int y = 0; y < dimy; y++)
                {
                    SolidColorBrush brush = new SolidColorBrush(ColorCollection[x, y]);
                    drawingContext.DrawRectangle(brush, null, new Rect(x * w, 0, w, this.ActualHeight));
                }
            }
        }
    }
}

The problem is that my control doesn't redraw itself when i change elements in the underlying array.问题是当我更改底层数组中的元素时,我的控件不会重绘自身。 It works fine when i assign a whole new array or resize the control though.当我分配一个全新的数组或调整控件大小时,它工作正常。

Obviously I need another class which somehow notifies control about internal changes in the collection.显然,我需要另一个 class 以某种方式通知控件有关集合中的内部更改。 I was looking at INotifyCollectionChange and ObservableCollection but the only articles I found were about binding collections to existing controls, not custom user-drawn ones.我正在查看 INotifyCollectionChange 和 ObservableCollection 但我发现的唯一文章是将 collections 绑定到现有控件,而不是自定义用户绘制的控件。 So I got confused and stuck at this point.所以我很困惑并停留在这一点上。

You probably can create a custom collection for yourself that works like your 2D array, but you need to also implement INotifyCollectionChange interface, which is not so hard to implement.您可能可以为自己创建一个自定义集合,其工作方式类似于您的 2D 数组,但您还需要实现 INotifyCollectionChange 接口,这并不难实现。 That way WPF will listen to your collection changes and update the control if necessary.这样 WPF 将监听您的集合更改并在必要时更新控件。

I think the sample may be useful.我认为样本可能有用。

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

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