自定义控件不在PropertyChanged上更新 [英] Custom control not updating upon PropertyChanged

查看:82
本文介绍了自定义控件不在PropertyChanged上更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自定义控件,其标题为其标题,其文本框为其InputText。控件绑定很好,但我似乎无法让它更新PropertyChanged事件。以下是我上课的方式:



  public   static   readonly  DependencyProperty HeaderProperty = DependencyProperty.Register( < span class =code-string>标题, typeof  string ),< span class =code-keyword> typeof (CustomControl), new  PropertyMetadata( null  )); 
public static readonly DependencyProperty InputTextProperty = DependencyProperty.Register( InputText typeof string ), typeof (CustomControl), FrameWorkPropertyMetadata( null ){BindsTwoWayByDefault = true });

public string 标题
{
get { return string )GetValue(HeaderProperty) ; }
set {SetValue(HeaderProperty, value ;}
}


public string InputText
{
< span class =code-keyword> get { return string )GetValue(InputTextProperty) );}
set {SetValue(InputTextProperty, value ;}
}





这里是ControlTemplate:



 <   StackPanel    方向  = 水平 >  
< TextBlock 文字 = {Binding Header,RelativeSource = {RelativeSource TemplatedParent}} 保证金 = 0,0,10,0 / >
< TextBox 文本 = {Binding InputText,RelativeSource = {RelativeSource TemplatedParent}} MinWidth = 150 / >
< / StackPanel >





我对控件的调用如下:



 <   local:CustomControl    标题  =  HeaderText     InputText   =   {Binding   InputString,    UpdateSourceTrigger   =   PropertyChanged /   >  





我错过了什么?

解方案
什么是你的DataContext在这里?只为DataContext对象中存在的属性实现INotifyPropertyChanged事件。


在TextBox Binding中,将UpdateSourceTrigger设置为PropertyChanged。代码如下:



 <  文本框    text   =  {Binding InputText,UpdateSourceTrigger = PropertyChanged,RelativeSource = {RelativeSource TemplatedParent}}    minwidth   =  150    /  >  


I'm trying out custom control that has a label for its header and a textbox for its InputText. The control binds fine but I can't seem toe get it to update on PropertyChanged event. Here is how I did the class:

public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header",typeof(string), typeof(CustomControl), new PropertyMetadata(null));
public static readonly DependencyProperty InputTextProperty = DependencyProperty.Register("InputText",typeof(string), typeof(CustomControl), new FrameWorkPropertyMetadata(null) {BindsTwoWayByDefault = true} );

public string Header
{
   get { return (string)GetValue(HeaderProperty); }
   set { SetValue(HeaderProperty,value; }
}


public string InputText
{
   get { return (string)GetValue(InputTextProperty); }
   set { SetValue(InputTextProperty,value; }
}



and here is the ControlTemplate:

<StackPanel Orientation="Horizontal">
   <TextBlock Text="{Binding Header, RelativeSource={RelativeSource TemplatedParent}}" Margin="0,0,10,0"/>
   <TextBox Text="{Binding InputText, RelativeSource={RelativeSource TemplatedParent}}" MinWidth="150"/>
</StackPanel>



my call for the control looks like this:

<local:CustomControl Header="HeaderText" InputText={Binding InputString, UpdateSourceTrigger=PropertyChanged/>



What did I miss?

解决方案

What is your DataContext here? just implement INotifyPropertyChanged event for your properties present in the DataContext object.


In TextBox Binding, set UpdateSourceTrigger to PropertyChanged. The code becomes like this:

<textbox text="{Binding InputText, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource TemplatedParent}}" minwidth="150" />


这篇关于自定义控件不在PropertyChanged上更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆