WPF自定义属性无法识别

问题描述

后台代码设定了自定义属性publicstaticreadonlyDependencyPropertyIsInEditModeProperty=DependencyProperty.RegisterAttached("IsInEditMode",typeof(bool),typeof(EditableTextBlock),newPropertyMetadata(false));

编辑后可以正常使用,但是XAML页面报错说无法识别成员<TriggerProperty="IsInEditMode"Value="True"><SetterProperty="ContentTemplate"Value="{StaticResourceEditModeTemplate}"/></Trigger>

这个是怎么回事?

解决方案

解决方案二:
还要添加getter和setterpublicstaticboolGetIsInEditMode(DependencyObjectobj){return(bool)obj.GetValue(IsInEditModeProperty);}publicstaticvoidSetIsInEditMode(DependencyObjectobj,boolvalue){obj.SetValue(IsInEditModeProperty,value);}

解决方案三:
用GETSET不行吗?publicboolIsInEditMode{get{if(IsEditable)return(bool)GetValue(IsInEditModeProperty);elsereturnfalse;}set{if(IsEditable){if(value)oldText=Text;SetValue(IsInEditModeProperty,value);}}}

解决方案四:
publicboolIsInEditMode{get{return(bool)GetValue(IsInEditModeProperty);}set{SetValue(IsInEditModeProperty,value);}}

解决方案五:
这个属性我也加了,但是前台报错
解决方案六:
XAML<UserControlx:Class="UserControls.EditableTextBlock"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:UserControls"x:Name="mainControl"><UserControl.Resources><DataTemplatex:Key="EditModeTemplate"><TextBoxKeyDown="TextBox_KeyDown"Loaded="TextBox_Loaded"LostFocus="TextBox_LostFocus"Text="{BindingElementName=mainControl,Path=Text,UpdateSourceTrigger=PropertyChanged}"Margin="0"BorderThickness="1"/></DataTemplate><DataTemplatex:Key="DisplayModeTemplate"><TextBlockText="{BindingElementName=mainControl,Path=FormattedText}"Margin="5,3,5,3"/></DataTemplate><StyleTargetType="{x:Typelocal:EditableTextBlock}"><Style.Triggers><TriggerProperty="IsInEditMode"Value="True"><SetterProperty="ContentTemplate"Value="{StaticResourceEditModeTemplate}"/></Trigger><TriggerProperty="IsInEditMode"Value="False"><SetterProperty="ContentTemplate"Value="{StaticResourceDisplayModeTemplate}"/></Trigger></Style.Triggers></Style></UserControl.Resources></UserControl>

后台代码usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;usingSystem.Windows.Threading;namespaceUserControls{publicpartialclassEditableTextBlock:UserControl{#regionConstructorpublicEditableTextBlock(){InitializeComponent();base.Focusable=true;base.FocusVisualStyle=null;}#endregionConstructor#regionMemberVariables//Wekeeptheoldtextwhenwegointoeditmode//incasetheuserabortswiththeescapekeyprivatestringoldText;#endregionMemberVariables#regionPropertiespublicstringText{get{return(string)GetValue(TextProperty);}set{SetValue(TextProperty,value);}}publicstaticreadonlyDependencyPropertyTextProperty=DependencyProperty.RegisterAttached("Text",typeof(string),typeof(EditableTextBlock),newPropertyMetadata(""));publicboolIsEditable{get{return(bool)GetValue(IsEditableProperty);}set{SetValue(IsEditableProperty,value);}}publicstaticreadonlyDependencyPropertyIsEditableProperty=DependencyProperty.RegisterAttached("IsEditable",typeof(bool),typeof(EditableTextBlock),newPropertyMetadata(true));publicdelegatevoidTextChange(stringoldStr,stringnewStr);publiceventTextChangeOnTextChange;publicboolIsInEditMode{get{if(IsEditable)return(bool)GetValue(IsInEditModeProperty);elsereturnfalse;}set{if(IsEditable){if(value)oldText=Text;SetValue(IsInEditModeProperty,value);}}}publicstaticreadonlyDependencyPropertyIsInEditModeProperty=DependencyProperty.RegisterAttached("IsInEditMode",typeof(bool),typeof(EditableTextBlock),newPropertyMetadata(false));publicstringTextFormat{get{return(string)GetValue(TextFormatProperty);}set{if(value=="")value="{0}";SetValue(TextFormatProperty,value);}}publicstaticreadonlyDependencyPropertyTextFormatProperty=DependencyProperty.RegisterAttached("TextFormat",typeof(string),typeof(EditableTextBlock),newPropertyMetadata("{0}"));publicstringFormattedText{get{returnString.Format(TextFormat,Text);}}#endregionProperties#regionEventHandlers//Invokedwhenweentereditmode.voidTextBox_Loaded(objectsender,RoutedEventArgse){TextBoxtxt=senderasTextBox;//GivetheTextBoxinputfocustxt.Focus();txt.SelectAll();}//Invokedwhenweexiteditmode.voidTextBox_LostFocus(objectsender,RoutedEventArgse){this.IsInEditMode=false;if(OnTextChange!=null)OnTextChange(oldText,Text);}//Invokedwhentheusereditstheannotation.voidTextBox_KeyDown(objectsender,KeyEventArgse){if(e.Key==Key.Enter){this.IsInEditMode=false;e.Handled=true;}elseif(e.Key==Key.Escape){this.IsInEditMode=false;Text=oldText;e.Handled=true;}}#endregionEventHandlers}}

解决方案七:
引用4楼ffsquare的回复:

这个属性我也加了,但是前台报错

...DependencyProperty.RegisterAttached...如果是attachedproperty,那么要用静态的getter/setter,因为attachedproperty并不是设置到声明类上。如果你要在声明类上起作用,要用depenencyproperty:DependencyProperty.Register(...),这种情况才可以用3楼的写法。
解决方案八:
引用6楼Forty2的回复:

Quote: 引用4楼ffsquare的回复:
这个属性我也加了,但是前台报错

...DependencyProperty.RegisterAttached...如果是attachedproperty,那么要用静态的getter/setter,因为attachedproperty并不是设置到声明类上。如果你要在声明类上起作用,要用depenencyproperty:DependencyProperty.Register(...),这种情况才可以用3楼的写法。

以前用的是Register,后来被我换成RegisterAttached了,我换回Register还是不行。
解决方案九:
用WPF的自定义控件,不用用户控件。

时间: 2024-08-03 15:40:17

WPF自定义属性无法识别的相关文章

【基于WPF+OneNote+Oracle的中文图片识别系统阶段总结】之篇二:基于OneNote难点突破和批量识别

      篇二:基于OneNote难点突破和批量识别 [1]开篇概述:在对本章技术介绍前,还是梳理下思路.描述下本章功能和开发过程.做个系统大致了解之后,在粘贴出本节效果图配以完整代码,然后分拆之,个体技术剖析.这样既可以达到全局整体效果,也可以对局部技术或者知识点做以小结.功能看图描述:本程序开发基于C#+WPF,然后这些条件具备前需要安装office2010以上版本,包含OneNote即可   完整代码:   namespace OnenoteOCRDemo { /// <summary>

【基于WPF+OneNote+Oracle的中文图片识别系统阶段总结】之篇三:批量处理后的txt文件入库处理

篇三:批量处理后的txt文件入库处理 [开篇概述]:本文继上述文章再做深度剖析,本篇主要介绍txt入库(oracle)和wpf分页,在此篇涉及的页面设计前篇已经细述不再概说.里面涉及到oracle 64位系统安装处理问题,以前oracle的表,序列,触发器,自增种子等基本操作.外加文件操作和利用用户控件分页等技术.大致效果图如下,其次粘贴完整代码,其后就一一概说本文核心要点和难点突破,最后梳理技术知识点,使其一则保持完整性,二则梳理整理知识点便于随时取用.(本篇暂做效果图如下,至于最终效果图和审

【基于WPF+OneNote+Oracle的中文图片识别系统阶段总结】之篇一:WPF常用知识以及本项目设计总结

篇一:WPF常用知识以及本项目设计总结 [1]开篇之始粘贴下WPF设计界面与前台代码如下:(后台实现以及内部分析放在第二篇) 本页面xaml完整代码: <Window x:Class="OnenoteOCRDemo.Main" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/200

c#中使用wpf做一个手写识别的程序

问题描述 在WPF中怎么删除当前在面板上显示已经写好的字?在点击事件中的代码是什么?学生求教,谢谢哦. 解决方案 解决方案二:Clear?解决方案三:你用的是WPF提供的InkCanvas吗?如果是,可以通过清除其Strokes集合来实现.比如:inkCanvas.Strokes.Clear();

WPF自定义控件的自定义属性如何在调用XAML中设置?

问题描述 我的自定义控件(MyControl)中定义了一个属性:privateButton_myButton=null;publicButtonMyButton{get{return_myButton;}set{_myButton=value;OnPropertyChanged(newPropertyChangedEventArgs("MyButton"));}}让它在调用界面的XAML中关联一个Button:<ButtonName="Button1"/>

【基于WPF+OneNote+Oracle的中文图片识别系统阶段总结】之篇四:关于OneNote入库处理以及审核

篇四:关于OneNote入库处理以及审核 [开篇概述]:文本作为系列文章的最后一篇,主要对页面进行设计布局和审核页面的完善,其中主页面用户可以查询审核通过的信息,浏览详细信息.也可以作为导航到orc页面,对批量图片进行文字处理,处理后的文件,可以通过入库页面对文件信息提取.达到半自动填写表格的状态,由于文件信息不一定符合主观要求或者格式不一致,所以设置了审核页面,也是信息修改页面,信息无误后,选择下面审核通过复选框即可成功入库.后面就是交付做数据挖掘处理了.效果如图所示 主页面:   完整代码:

wpf RichTextBox GetLineStartPosition 在某种情况下不能识别换行?

问题描述 TextPointertp1=rtb.Selection.Start.GetLineStartPosition(1);这样可以选中我选择行的下一行的TextPointer,ok,没问题.但是:TextPointert1=rtb.Document.ContentStart.GetLineStartPosition(1);我这样使用,却不能取到下一行的TextPointer.他会引发一个异常,后来发现,这种情况下,所有的内容(不管你有几行)都会被当做一个只有一行的超长字符串(其中包括换行符

WPF学习之资源-Resources

WPF通过资源来保存一些可以被重复利用的样式,对象定义以及一些传统的资源如二进制数据,图片等等,而在其支持上也更能体现出这些资源定义的优越性.比如通过ResourceDictionary的支持就可以通过资源来实现换肤功能,在ExpressionBlend中设计的酷炫造型也可以通过导出成资源来很容易的被程序员所引用,本地化的实现,访问另外程序集的嵌入式资源等等.这些都给我们提供了丰富的手段通过资源访问架构来构建丰富的富媒体应用程序.本文简单讲解了WPF Resources的分类及其常见用法,并简单

WPF 字体、颜色等属性的序列化和反序列化

WPF自带的一些属性比如System.Windows.Media.Brush.System.Windows.Media.FontFamily.System.Windows.FontWeight等不具有直接序列化能力,本文针对这一点设计名为PropertySerializateDictionary的类,实现了不同类不同对象公有属性的序列化和反序列化.本类继承于Dictionary<string, TypeAndValue>, IXmlSerializable,调用了自实现类TypeAndValu