XAML binding against Auto-Implemented properties?

我只是一个虾纸丫 提交于 2021-02-04 21:05:55

问题


Is it possible to using XAML data binding to a model with Auto-Implemented properties?

class ClassA
{
  // pseudo code.
  int Width { get; set{ NotifyPropertyChange("Width");} }
}


//XAML
<textBox width="{Binding Path=Width,Mode=OneWay}"/>

回答1:


Auto-properties don't have a half auto mode. It needs to either be an auto-property with nothing extra:

int Width { get; set; }

or a fully expanded property with a backing store that can have additional code added to it, like change notification:

int _width;
int Width
{
  get { return _width; }
  set
  {
    _width = value;
    NotifyPropertyChange("Width");
  }
}

If you use auto-properties you can still bind to them but you're giving up change notification, so any changes you make to the property from code won't show up in the UI. In general any object being used for data binding should include change notification and so should not use auto-properties.




回答2:


i interprete Autoproperty this way.

class ClassA
{
  int Width { get; set;}
}

Yes one-way binding to view is always possible.

Twoway binding require NotifyPropertyChange("propertyname"); only if you want that changes in one modell element cause automatic update of the gui or other observers.

There is tool that can autogeneretates INotifyPropertyChange implementation for you: notifypropertyweaver.

Update

There is also a INotifyPropertyChange-Free altenative for two way binding described in code-magazine article "INotifyPropertyChanged Is Obsolete" using the free lib updatecontrols on codeplex




回答3:


Your example is not a Auto-implemented property, in fact it will probably not compile.

To implement this you would need to either implement a full backing-store property or use aspects to implement the INotifyPropertyChanged so that you can keep your Auto property clean.




回答4:


Yes you can use xaml binding with Auto properties. But as said, the property illustrated is not an Auto property.



来源:https://stackoverflow.com/questions/5415144/xaml-binding-against-auto-implemented-properties

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!