简体   繁体   English

如何在WPF自定义控件库上显示画布?

[英]how to display a canvas on a wpf custom control library?

I have a wpf custom control which draws stuff on a canvas. 我有一个wpf自定义控件,可在画布上绘制内容。 but i cant figure out how to display this canvas on the window. 但我不知道如何在窗口上显示此画布。 When i add this control to a host app, I want this canvas to fill that app's window. 当我将此控件添加到主机应用程序时,我希望此画布填充该应用程序的窗口。 I have seen some tutorial which use template binding to display ui elements etc but i read that canvas doesnt have a template property or something... How can display this canvas that I am drawing on? 我看过一些使用模板绑定来显示ui元素等的教程,但我读到画布没有模板属性或其他东西。如何显示我正在绘制的画布? thanks 谢谢

 namespace WPFCustomControls { 

       public class MyCustomControl : Control { 
           private Canvas canvas = new Canvas();

          static MyCustomControl() { 
             DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new
                FrameworkPropertyMetadata(typeof(MyCustomControl))); 
          } 

             public void drawOnCanvas()
              {
                 //draw stuff on canvas
              }
               public void displayCanvas()
              {
                 //display canvas on app window
              }

       } 
    } 

Instead of deriving from Control , you could derive from ContentControl which has a Content property. 除了从Control派生,还可以从具有Content属性的ContentControl派生。

For example: 例如:

public class MyCustomControl : ContentControl 
{ 
    private Canvas _canvas = new Canvas();

    public MyCustomControl()
    {
        this.Content = _canvas;
    }
} 

It's also possible to derive from Canvas directly. 也可以直接从Canvas派生。

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

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