ContentControl의 Content 속성을 DrawingVisual 개체로 설정할 수 있습니까? 문서에서 내용은 아무것도 될 수는 있지만 캔버스에 컨트롤을 추가하면 아무 것도 나타나지 않는다고 말합니다. 가능한 일입니까? 콘텐츠가 DrawingVisual 인 ContentControl을 캔버스에 추가하는 전체 코드를 게시 할 수 있습니까?WPF - ContentControl 내용을 DrawingVisual로
2
A
답변
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 측
관련 문제
- 1. WPF ContentControl Styling
- 2. StackPanel을 ContentControl (WPF)로 사용
- 3. WPF - 캔버스 내에서 ContentControl 자르기
- 4. WPF - 사용자 지정 캔버스에 ContentControl 추가
- 5. ContentControl 숨기기
- 6. 상속 된 ContentControl 요소에 ContentControl 리소스 할당
- 7. WPF : 중첩 된 DrawingGroup을 중첩 된 DrawingVisual로 변환
- 8. ContentControl + RenderTargetBitmap + empty image
- 9. ContentControl 크기 조정 문제
- 10. WPF TreeView가 내용을 표시하지 않습니다.
- 11. WPF WebBrowser - 내용을 확대하는 방법?
- 12. ContentControl (Caliburn.Micro)에 따라 다른보기
- 13. ContentControl 필드가있는 사용자 정의 UserControl
- 14. Grid of ContentControl 요소를 원하십니까?
- 15. WPF ListView에서 Observable 컬렉션의 내용을 표시하지 않습니다.
- 16. WPF ScrollViewer의 내용을 특정 위치로 스크롤하는 방법
- 17. WPF DataGrid : 단일 셀의 내용을 어떻게 얻습니까?
- 18. WPF 폼의 내용을 표시하기 위해 크기가 조정되어야합니다.
- 19. WPF DataGrid의 변경 내용을 DataTable에 저장
- 20. WPF : UserControl 안에있는 DockPanel의 내용을 편집하려면 어떻게해야합니까?
- 21. 접힌 WPF 확장기에서 내용을 클릭하는 방법?
- 22. ContentControl 및 해당 하위 - 동일한 크기?
- 23. WPF보기 contentControl 바운드 속성 변경시 렌더링되지 않음
- 24. WPF User Control 확장 경계 클래스입니다. "직접 내용을 지원하지 않습니까?"
- 25. WPF ContentControl : .cs 파일의 비활성화 된 모드에서 컨트롤 불투명도를 변경하는 방법?
- 26. WPF 트리거
- 27. WPF 트리거에서 Grid.ColumnSpan 변경
- 28. Silverlight에서 탭 페이지의 내용을 어떻게 배치합니까?
- 29. WPF : XPSPackagingException
- 30. WPF. TwoState 요소