更改使用DrawingVisual的本地类的属性

时间:2014-08-03 09:00:08

标签: c# wpf xaml drawingvisual

我定义了一个Drawing类,它派生自FrameworkElement。我使用它来绘制DrawingVisual。在我XAML我已经包含了这个本地课程。

现在我的问题是如何将此类的属性绑定到变量,或者我怎样才能更改它们?

例如,假设我要更改width属性。我在Button上添加了XAML,并试图更改width,但没有任何反应。

所有必需的代码如下:

<Window x:Class="Geom.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:geom="clr-namespace:Geom"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="100"/>
        </Grid.RowDefinitions>
        <Canvas Width="0" Height="0">
            <geom:Drawing x:Name="Drawing" />
        </Canvas>
        <Button Grid.Row="1" Content="Change" Click="ButtonBase_OnClick"/>
    </Grid>
</Window>


public class Drawing : FrameworkElement
{
    private readonly VisualCollection _visuals;
    public int width { get; set; }

    public Drawing()
    {
        RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
        _visuals = new VisualCollection(this);

        var geometryGroup = new GeometryGroup();
        width = 50;
        var depth = 50;
        var x = 50;
        var y = 50;
        var rect = new Rect(x, y , width, depth);
        var rectGeom = new RectangleGeometry(rect);
        geometryGroup.Children.Add(rectGeom);
        geometryGroup.Freeze();

        var drawingVisual = new DrawingVisual();
        using (var dc = drawingVisual.RenderOpen())
        {
            dc.DrawGeometry(Brushes.Blue, null, geometryGroup);
        }
        _visuals.Add(drawingVisual);
    }

    protected override Visual GetVisualChild(int index)
    {
        return _visuals[0];
    }

    protected override int VisualChildrenCount
    {
        get
        {
            return 1;
        }
    }
}


public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        Drawing.width = 10;
    }
}

1 个答案:

答案 0 :(得分:1)

要使DrawingVisual刷新,您必须 使用新值在其drawingContext上重新绘制视觉

因此,如果您设置宽度,则必须在其绘图上下文中重新绘制视觉效果。将您的代码更改为:

public class Drawing : FrameworkElement
{
    private readonly VisualCollection _visuals;

    private int width;
    public int VisualWidth
    {
        get { return width; }
        set
        {
            width = value;
            RefreshDrawing();
        }
    }

    public Drawing()
    {
        RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
        _visuals = new VisualCollection(this);

        var geometryGroup = new GeometryGroup();
        width = 50;
        DrawingVisual drawingVisual = new DrawingVisual();
        RefreshDrawing(drawingVisual);
        _visuals.Add(drawingVisual);
    }

    private void RefreshDrawing(DrawingVisual drawingVisual = null)
    {
        var geometryGroup = new GeometryGroup();
        var rect = new Rect(50, 50, VisualWidth, 50);
        var rectGeom = new RectangleGeometry(rect);
        geometryGroup.Children.Add(rectGeom);
        geometryGroup.Freeze();

        drawingVisual = drawingVisual ?? (DrawingVisual)GetVisualChild(0);
        using (var dc = drawingVisual.RenderOpen())
        {
            dc.DrawGeometry(Brushes.Blue, null, geometryGroup);
        }
    }

    protected override Visual GetVisualChild(int index)
    {
        return _visuals[0];
    }

    protected override int VisualChildrenCount
    {
        get
        {
            return 1;
        }
    }
}

从按钮点击处理程序设置 VisualWidth

Drawing.VisualWidth = 10;