2011-01-08 2 views
2

ContentControl의 Content 속성을 DrawingVisual 개체로 설정할 수 있습니까? 문서에서 내용은 아무것도 될 수는 있지만 캔버스에 컨트롤을 추가하면 아무 것도 나타나지 않는다고 말합니다. 가능한 일입니까? 콘텐츠가 DrawingVisual 인 ContentControl을 캔버스에 추가하는 전체 코드를 게시 할 수 있습니까?WPF - ContentControl 내용을 DrawingVisual로

답변

4

ContentControl의 Content 속성을 DrawingVisual 개체로 설정할 수 있습니까?

기술적으로 예, 가능합니다. 그러나, 그것은 아마도 당신이 원하는 것이 아닙니다. ContentControl에 추가 된 DrawingVisual은 단순히 "System.Windows.Media.DrawingVisual"문자열을 표시합니다. 그리드 내에서 다음 코드는 easilly이를 보여줄 것입니다 :

<Button> 
    <DrawingVisual/> 
</Button> 

가 제대로으로 DrawingVisual을 사용하려면 FrameworkElement 내에서 캡슐화 할 필요가있다. Microsoft Reference을 참조하십시오.

따라서 다음 코드는 원하는 작업을 도와줍니다.

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]; 
    } 

} 
} 
:
<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# 1 측

관련 문제