Silverlight数据绑定之 绑定一个int类型的属性
还就真心不会啊!
在类FunctionPanel中作如下定义:
/// <summary> /// 鼠标状态 属性 /// </summary> public DependencyProperty nMouseFlagProperty;
public int nMouseFlag { get { return int.Parse(GetValue(nMouseFlagProperty).ToString()); } set { SetValue(nMouseFlagProperty, value); } }
再注册一下属性:
public FunctionPanel() { nMouseFlagProperty = DependencyProperty.Register("nMouseFlag", typeof(int), typeof(FunctionPanel), new PropertyMetadata(null)); ..................... }
随后,在MainPageLoaded中代码绑定
MainPageLoaded() { ......... /************************************************************************/ /* 绑定数据 */ /************************************************************************/ Binding bind1 = new Binding(); bind1.Source = m_nMouseLeftFlag; bind1.Path = new PropertyPath("nMouseFlag"); bind1.Mode = BindingMode.TwoWay; panel.SetBinding(panel.nMouseFlagProperty, bind1); panel.nMouseFlag = 2; }
但是绑定失败,没有什么效果!
最后,只能保留
public int nMouseFlag { get; set; }
直接在MainPageLoaded(){}中赋值
MainPageLoaded() {
.............. panel.nMouseFlag = m_nMouseLeftFlag; }