首页 新闻 赞助 找找看

第一次加载界面时,TextBox没有显示viewmodel中属性绑定的值,自定义TextBox,mvvm,WPF

0
悬赏园豆:50 [已解决问题] 解决于 2014-08-15 09:16

我自定义一个只能输入数字的TextBox,现在Textbox能够只输入数字。TextBox与我在ViewModel定义的属性绑定在一起,问题是最开始页面加载,TextBox没有显示数值,

自定义的依赖属性如图

绑定的ViewModel如图

引用自定义的Textbox代码

自定义的Textbox代码

扎伊尔天雕的主页 扎伊尔天雕 | 初学一级 | 园豆:7
提问于:2014-08-10 16:58
< >
分享
最佳答案
0

ValueProperty依赖属性注册的时候指定PropertyMetadata,在PropertyChangedCallback中赋值

收获园豆:50
jello chen | 大侠五级 |园豆:7306 | 2014-08-10 19:50

在PropertyChangedCallback怎么给依赖属性赋值啊?谢谢

扎伊尔天雕 | 园豆:7 (初学一级) | 2014-08-12 01:06

@扎伊尔天雕: 大致如下:

public static DependencyProperty ValueProperty = DependencyProperty.Register("Value",typeof("string"),typeof(TextBoxTemplate),new PropertyMetadata(OnValueChanged));

public static void OnValueChanged(DependencyObject sender,DependencyPropertyChangedEventArgs e)

{

  TextBoxTemplate tb = (TextBoxTemplate)sender;

  string oldValue = (string)e.OldValue;

  string newValue = (string)e.NewValue;

  RoutedPropertyChangedEventArgs<string> ex = new RoutedPropertyChangedEventArgs<string>(oldValue,newValue,TextBoxTemplate.ValueProperty);

tb.RaiseEvent(ex);

}

试下,不确定一定可行

jello chen | 园豆:7306 (大侠五级) | 2014-08-12 15:22

@jello chen: 报错了,The best overloaded method match for 'System.Windows.RoutedPropertyChangedEventArgs<string>.RoutedPropertyChangedEventArgs(string, string, System.Windows.RoutedEvent)' has some invalid arguments

扎伊尔天雕 | 园豆:7 (初学一级) | 2014-08-12 22:45

@扎伊尔天雕: 不好意思,没在IDE写,参数记错了,试试这样

RoutedPropertyChangedEventArgs<string> ex = new RoutedPropertyChangedEventArgs<string>(oldValue,newValue);

tb.RaiseEvent(ex);

jello chen | 园豆:7306 (大侠五级) | 2014-08-13 16:53

@扎伊尔天雕: 如果不行,方便的话,将你自定义的TextBox发我调试下

jello chen | 园豆:7306 (大侠五级) | 2014-08-13 16:57

@jello chen: 已经调通了,非常谢谢的回复!

扎伊尔天雕 | 园豆:7 (初学一级) | 2014-08-15 09:16

@扎伊尔天雕: 很高兴能帮到你

jello chen | 园豆:7306 (大侠五级) | 2014-08-17 20:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册