尝试编辑类型化DataTemplate时出现奇怪的绑定错误 [英] Weird binding error while trying to edit typed DataTemplate

查看:58
本文介绍了尝试编辑类型化DataTemplate时出现奇怪的绑定错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个ViewModel(TestViewModel),它有一个只读的 """"属性。在这个ViewModel中,我有一个名为Model(TestModel)的属性,它也有一个"Name"。属性不是只读的。然后我定义一个DataTemplate为TestViewModel到的它的模型属性
显示名称和说明字段


现在,当我从"打开MainWindow.xaml;表达混合"并从资源选项卡尝试编辑此DataTemplate我收到以下错误:


" TwoWay或OneWayToSource绑定无法在'DataTemplateError类型的只读属性'Name'上工作。 TestViewModel'。"


DataTemplate在项目运行时按预期工作;所以绑定是正确的。但是当尝试通过Blend编辑/设计DataTemplate时,它无法正常工作。虽然我将StackPanel的DataContext设置为Model,但它给出的错误就像TextBox
for """"属性 结合至 仅"读;姓名"  TestViewModel的属性:


我提供以下必要的代码的文件和我使用表达混合4 SP1

提前致谢


--------------------------- -------------------------------------------------- -------------------------------------------------- ---------------------------------------------


TestModel类如下:

 使用  System; 
使用  System.ComponentModel;

命名空间  DataTemplateError  {

   public   class   < span style ="color:#2b91af"> TestModel  :  INotifyPropertyChanged   {

   ;   private   string   _Name  =  " Runtime  Model  Name" ;
  ;&NBSP;&NBSP;&NBSP;的私有&NBSP;的&NBSP; _Description&NBSP; =&NBSP;的"运行时 型号 描述" ;

     public   string  名称  {
       get   {  return   ._名称; }
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的设置&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; _Name  =  value ;
         this .NotifyPropertyChanged("姓名" );
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}
&NBSP;&NBSP;&NBSP;&NBSP;}

     public   string  说明  {
       get   {  return   ._说明; }
       设置  {
         _Description  =  value ;
         this .NotifyPropertyChanged(" Description" ; );      }
    }

    ;  public   event &n bsp; PropertyChangedEventHandler   PropertyChanged;

     private   void   NotifyPropertyChanged( String   info)  {
       if  (PropertyChanged !=  null )  {
         PropertyChanged( this ,  new   PropertyChangedEventArgs (info));
       }    }

  }
}

TestViewModel类如下:


 使用 系统; 
使用  System.ComponentModel;

< span style ="color:blue">命名空间  DataTemplateError  {

   public   class   TestViewModel  :  INotifyPropertyChanged   {

     private   string   _Name  =  " Runtime  ViewModel  Name" ;

     public   string  名称  {
      ;  get   {  return   这个 ._名称; }
       private   设置&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP ;     _Name  =  value ;
         .NotifyPropertyChanged(" Name" );
     ;  }    }

     private   TestModel   _Model  =  new   TestModel ();

     public   TestModel  型号  {
       get   {  ; 返回  ._型号; }
     ;   设置  {
     &n bsp;   _Model  =  value ;
         .NotifyPropertyChanged(" Model" );
       }    }

     public   < span style ="color:blue"> void
  UpdateNameMethod()  {
       if  (! this .Name.EndsWith(" Updated  Value" ,  ; StringComparison .Ordinal))  {
         Name  =  Name  ; +  "  -  已更新 值" ;      }
    }

   &n bsp; public   event   PropertyChangedEventHandler   PropertyChanged;

     private   void   NotifyPropertyChanged( String   info)  {
       if  (PropertyChanged !=  null )  {
    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的PropertyChanged(的,&NBSP;的&NBSP; <跨度风格= "颜色:#2b91af"> PropertyChangedEventArgs (信息));
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}
&NBSP;&NBSP;&NBSP;&NBSP;} <无线电通信/>  }
}

解决方案

看起来好像是因为它实际上是针对您的Test上的Name属性ViewModel类,具有私有集方法:


 


 public string Name {
get {return this._Name; }
private set {
_Name = value;
this.NotifyPropertyChanged(" Name");
}
}


Hi,

I have a ViewModel (TestViewModel) which has a read only "Name" property. In this ViewModel I have property named Model(TestModel) which also has a "Name" property which is NOT read only. Then I defined a DataTemplate for TestViewModel to show Name and Description fields of it's Model property.

Now when I open MainWindow.xaml from "Expression Blend" and from the resources tab try to edit this DataTemplate I get the following error:

"A TwoWay or OneWayToSource binding cannot work on the read-only property 'Name' of type 'DataTemplateError.TestViewModel'."

The DataTemplate works as expected when the project runs; so the bindings are correct. But when trying to edit/design DataTemplate through Blend it's not working. Although I set the DataContext of StackPanel to Model it's giving the error as if the TextBox for "Name" property is bound to read only "Name" property of TestViewModel:

I've supplied the necessary code files below and I'm using Expression Blend 4 SP1.

Thanks in advance

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

TestModel class is below:

using System;
using System.ComponentModel;

namespace DataTemplateError {

  public class TestModel : INotifyPropertyChanged {

    private string _Name = "Runtime Model Name";
    private string _Description = "Runtime Model Description";

    public string Name {
      get { return this._Name; }
      set {
        _Name = value;
        this.NotifyPropertyChanged("Name");
      }
    }

    public string Description {
      get { return this._Description; }
      set {
        _Description = value;
        this.NotifyPropertyChanged("Description");
      }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info) {
      if (PropertyChanged != null) {
        PropertyChanged(thisnew PropertyChangedEventArgs(info));
      }
    }

  }
}

TestViewModel class is below:

using System;
using System.ComponentModel;

namespace DataTemplateError {

  public class TestViewModel : INotifyPropertyChanged {

    private string _Name = "Runtime ViewModel Name";

    public string Name {
      get { return this._Name; }
      private set {
        _Name = value;
        this.NotifyPropertyChanged("Name");
      }
    }

    private TestModel _Model = new TestModel();

    public TestModel Model {
      get { return this._Model; }
      set {
        _Model = value;
        this.NotifyPropertyChanged("Model");
      }
    }

    public void UpdateNameMethod() {
      if (!this.Name.EndsWith("Updated Value"StringComparison.Ordinal)) {
        Name = Name + " - Updated Value";
      }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info) {
      if (PropertyChanged != null) {
        PropertyChanged(thisnew PropertyChangedEventArgs(info));
      }
    }

  }
}

解决方案

It looks like it is because it is actually targeting the Name property on your TestViewModel class, which has a private set method:

 

 public string Name {
   get { return this._Name; }
   private set {
    _Name = value;
    this.NotifyPropertyChanged("Name");
   }
  }


这篇关于尝试编辑类型化DataTemplate时出现奇怪的绑定错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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