错误:“ResourceDictionary”根元素需要 x:Class 特性来支持 XAML 文件中的事件处理程序。请移除 MouseLeftButtonDown 事件的事件处理程序.
原文:错误:“ResourceDictionary”根元素需要 x:Class 特性来支持 XAML 文件中的事件处理程序。请移除 MouseLeftButtonDown 事件的事件处理程序.
在资源字典中设置listboxItem的鼠标左击的事件样式。
打出这段代码提示“ResourceDictionary”根元素需要 x:Class 特性来支持 XAML 文件中的事件处理程序。请移除 MouseLeftButtonDown 事件的事件处理程序,或将 x:Class 特性添加到根元素。 ”错误,
这句话是什么意思?难道EventSetter 不能在资源字典中写?
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1">
<Style x:Key="remenber" TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="1"></Setter>
<EventSetter Event="MouseLeftButtonDown" Handler="ProjectMouseLeftButtonDown"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#FF569BEE"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
解决思路:
1.首先,EventSetter 是可以在资源字典中写的。那句提示意思是需要在ResourceDictionary标签内加上x:Class特性。
你可以写成这样:
ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
x:Class="命名空间.资源字典的名称" >
</ResourceDictionary>
命名空间:以你的代码为例,此处应为”WpfApplication1”
资源字典的名称:如果资源字典文件是”Dictionary1.xaml”,这里就是”Dictionary1”
完整写法就是 x:Class=”WpfApplication1. Dictionary1”
2.下面的Demo供你参考:
Dictionary1.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WriteEventInResourceDictionary"
x:Class="WriteEventInResourceDictionary.Dictionary1">
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="MyCustomMouseEvent"/>
</Style>
</ResourceDictionary>
Dictionary1.xaml.cs:
namespace WriteEventInResourceDictionary
{
public partial class Dictionary1
{
private void MyCustomMouseEvent(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello");
}
}
}
MainWindow.xaml:
<ListBox>
<ListBoxItem>Item 1</ListBoxItem>
<ListBoxItem>Item 2</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
</ListBox>
App.xaml:
<Application x:Class="WriteEventInResourceDictionary.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WriteEventInResourceDictionary"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>错误:“ResourceDictionary”根元素需要 x:Class 特性来支持 XAML 文件中的事件处理程序。请移除 MouseLeftButtonDown 事件的事件处理程序.的更多相关文章
- Oracle 远程访问配置 在 Windows Forms 和 WPF 应用中使用 FontAwesome 图标 C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素” C#[Win32&WinCE&WM]应用程序只能运行一个实例:MutexHelper Decimal类型截取保留N位小数向上取, Decimal类型截取保留N位小数并且不进行四舍五入操作
Oracle 远程访问配置 服务端配置 如果不想自己写,可以通过 Net Manager 来配置. 以下配置文件中的 localhost 改为 ip 地址,否则,远程不能访问. 1.网络监听配置 ...
- windows phone xaml文件中元素及属性(10)
原文:windows phone xaml文件中元素及属性(10) Textblock xaml文件和隐藏文件 在设计界面的时候我们可以通过xaml中进行设计,这种设计是所见即所得的,很是方便,由于x ...
- C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素”
Q: 在反序列化 Xml 字符串为 Xml 对象时,抛出如下异常. 即在 XML文档(0, 0)中有一个错误:缺少根元素. A: 首先看下代码: StringBuilder sb = new Stri ...
- 微信小程序循环中点击一个元素,其他的元素不发生变化,类似点击一个循环中的语音,其他的不发生点击事件
类似语音,因为都在一个数据内,所以点击第一个,所有的语音都变化,解决方法就是 把整个数据都获取下来,然后更改其中一个需要更改的值,然后再把整个数据都setdata回去,如果需要动画的话,wxml里面放 ...
- Goldengate 12.2新特性-自描述的队列文件
OGG12.2中最大的变化之一就是队列文件是自描述的,意思是不再担心以前版本中,表结构异构的情况,也不再需要defgen生成定义文件,以及不再使用assumeTargetDefs或SourceDefs ...
- SharePoint 2013 Workflow Manager 1.0 远程服务器返回错误: (400) 错误的请求。 不支持查询字符串中的 api-version
环境: Windows Server 2012 R2 Standard SharePoint Server 2013 with sp1 通过Web 平台安装程序 5.0,已安装 Workflow Ma ...
- spring整合mybatis错误:Caused by: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 62; 文档根元素 "mapper" 必须匹配 DOCTYPE 根 "configuration"。
运行环境:jdk1.7.0_17+tomcat 7 + spring:3.2.0 +mybatis:3.2.7+ eclipse 错误:Caused by: org.xml.sax.SAXParseE ...
- 浏览器打开aspx文件 ,提示:XML 解析错误:找不到根元素
在使用VS2013这个IDE创建aspx文件后,在浏览器打开后居然发现了以下错误:XML 解析错误:找不到根元素 详细查看里面的信息,发现了这么一个链接位置:http://localh ...
- asp.net mvc 碰到 XML 解析错误:找不到根元素 位置
具体报错信息如下: XML 解析错误:找不到根元素 位置:moz-nullprincipal:{4a1d2b7c-6d07-468e-9df9-2267a0422c93} 行 1,列 1: 网上给出的 ...
随机推荐
- FullPage.js全屏滚动插件解说
1.主要功能 1).支持鼠标滚动 2).多个回调函数 3).支持手机.平板触屏事件 4).支持css3动画 5).支持窗口缩放 6).窗口缩放时自动调整 7).可设置滚动宽度.背景颜色.滚动速度.循环 ...
- [Typescript] What is a Function Type ? Function Types and Interfaces - Are They Related ?
Function Type: type messageFn = (name: string) => string; function sayHello(name: string): string ...
- TCP协议的一些认识及实践
http://www.2cto.com/net/201210/163047.html 一.简介 引用<TCP/IP详解-卷1>中的介绍,TCP与UDP使用相同的网络层(IP层),TCP却向 ...
- 【BZOJ 1008】[HNOI2008]越狱
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1008 [题意] [题解] 相邻就会犯罪的话; 可以考虑它的反面; 即让所有相同信仰的人 ...
- lipo: can't open input file
错误1: /Volumes/Mac OS/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/u ...
- 【noip模拟】太空电梯 贪心
题目大意 人数n,电梯载重k,电梯限制人数2,给出每个人的体重v,求按照怎样的顺序排队电梯运送的次数越多. 题解 排序后,每次都先选择最小的,然后看最大的上来是否超出载重, 若超出,则这两个对答案贡献 ...
- 【22.73%】【codeforces 606D】Lazy Student
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 重装huson遇到的一些错误及解决
作者:朱金灿 来源:http://blog.csdn.net/clever101 服务器换了一块新硬盘,拷贝回原来的数据后结果发现Apache tomcat服务不能启动了,错误提示是:本地计算机上的A ...
- 从vue1迁移到vue2踩到的两个坑
先说第一个,在vue1中用v-for的时候,习惯性用$index和$key来取键.今天迁移到vue2之前,也知道vue2里不能这样用了,结果还是出问题了, 数据渲染不出来. <li v-for= ...
- STL关联式容器之set和multiset
一,set和multiset的基础知识 1.set和multiset的基础 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按照一定的顺序排列,元素插入过程是按照排序规则插入的.所以不能指 ...