簡體   English   中英

WPF-ContentControl內容為DrawingVisual

[英]WPF - ContentControl Content as DrawingVisual

我可以將ContentControl的Content屬性設置為DrawingVisual對象嗎? 它在文檔中說,內容可以是任何東西,但我嘗試過,將控件添加到畫布時沒有任何顯示。 是否可以?如果可以,您是否可以發布將內容控件為DrawingVisual的ContentControl添加到畫布的完整代碼?

我可以將ContentControl的Content屬性設置為DrawingVisual對象嗎?

從技術上講,是的,您可以。 但是,這可能不是您想要的。 添加到ContentControl的DrawingVisual將僅顯示字符串“ System.Windows.Media.DrawingVisual”。 網格中的以下代碼將演示此操作:

<Button>
    <DrawingVisual/>
</Button>

為了正確使用DrawingVisual,您需要將其封裝在FrameworkElement中。 請參閱Microsoft參考

因此,以下代碼將幫助您完成所需的工作。

<Window x:Class="TestDump.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestDump"
Title="Window1" Height="300" Width="600" >
<Grid>
    <Canvas>
        <Button >
            <local:MyVisualHost Width="600" Height="300"/>
        </Button>
    </Canvas>
</Grid>
</Window>

在C#方面:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestDump
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }
}

public class MyVisualHost : FrameworkElement
{
    private VisualCollection _children;
    public MyVisualHost()
    {
        _children = new VisualCollection(this);
        _children.Add(CreateDrawingVisualRectangle());
    }
    // Create a DrawingVisual that contains a rectangle.
    private DrawingVisual CreateDrawingVisualRectangle()
    {
        DrawingVisual drawingVisual = new DrawingVisual();

        // Retrieve the DrawingContext in order to create new drawing content.
        DrawingContext drawingContext = drawingVisual.RenderOpen();

        // Create a rectangle and draw it in the DrawingContext.
        Rect rect = new Rect(new System.Windows.Point(160, 100), new System.Windows.Size(320, 80));
        drawingContext.DrawRectangle(System.Windows.Media.Brushes.Blue, (System.Windows.Media.Pen)null, rect);

        // Persist the drawing content.
        drawingContext.Close();

        return drawingVisual;
    }

    // Provide a required override for the VisualChildrenCount property.
    protected override int VisualChildrenCount
    {
        get { return _children.Count; }
    }

    // Provide a required override for the GetVisualChild method.
    protected override Visual GetVisualChild(int index)
    {
        if (index < 0 || index >= _children.Count)
        {
            throw new ArgumentOutOfRangeException();
        }

        return _children[index];
    }

}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM