c# - 从 Canvas 继承

标签 c# wpf xaml inheritance namespaces

我想实现一个允许拖动对象的 Canvas 。所以我想我可以从 Canvas 继承并覆盖鼠标事件:

namespace ISC_CG
{
    public class DragCanvas : Canvas
    {
        protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
        ...

在 XAML 中:
<Window x:Class="ISC_CG.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="mainWindow"
        DataContext="{Binding ElementName=mainWindow}"
        xmlns:cg="clr-namespace:ISC_CG"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        Title="MainWindow" Height="350" Width="525">
    <cg:DragCanvas Name="mainCanvas" Background="{StaticResource checkers}" ClipToBounds="True" Focusable="True">

但我收到错误:

The name "DragCanvas" does not exist in the namespace "clr-namespace:ISC_CG".



我在这里错过了一些步骤吗?

谢谢。

最佳答案

仅供引用 - 这是我修复它的方法:

  • 我必须删除代码隐藏中对“mainCanvas”的所有引用
  • 然后我重新编译并得到一个新的 Xaml 错误:在行中


  • 告诉我我必须用“x:Name”替换“Name”。

    关于c# - 从 Canvas 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21467044/

    相关文章:

    c# - 查找字符串中最长的数字序列

    C# 无法链接 LinkedList 中的节点

    c# - WCF REST 服务客户端配置文件为空

    wpf - 使用 MVVM-Light 的消息传递问题

    wpf - 使用MVVM在WPF DataGrid中显示组合框的用户友好方法

    WPF:ScrollViewer 和 Grid - 禁用 TexBox 调整大小

    c# - LongListMultiSelector 阻止所选项目的手势事件

    c# - 使用 C# 从 BitmapData 裁剪区域

    .net - 设计模式下的XamlParseExceptions-使设计模式无用

    wpf - WPF 应用程序的配色方案