问题描述
后台代码设定了自定义属性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的自定义控件,不用用户控件。