MVVM Refresh a ReadOnly Textbox in a System.Windows.Controls.Grid
I have a Grid :
<Grid Name="MyGrid" DataContext="{Binding MyG开发者_Python百科ridBinded}" >
<TextBox Text="{Binding Path=Total, Mode=OneWay}" />
</Grid>
And MyGridBinded is an object:
public class GridPOCO
{
public IList myList { get ; set; }
public Total { get { Return myList.Count; }}
}
So when i update my myList, in My ViewModel I call :
Base.OnpropertyChanged("MyGridBinded");
But Total is not updated.
How can i do to update my readonly field ?
@H.B and @Daniel Rose.
If I implement INotifyPropertyChanged in GridPOCO. I could after call :
Base.OnpropertyChanged("Total");
But GridPOCO is a POCO object, and i don't want to implement INotifyPropertyChanged in it.
When I would have time, I will try the Ben answer.
You need to name the property, not the object:
Base.OnpropertyChanged("Total");
(The object containing the Total
proeprty or one of its base classes needs to implement INotifyPropertyChanged and that method needs to be called on the instance whose property changed)
Alternatively just call it with null
to update all property bindings (you normally avoid this):
Base.OnpropertyChanged(null);
I think you probably need:
<TextBox Text="{Binding Path=MyGridBinded.Total, Mode=OneWay}" />
And also to call the property changed event for the property (clue is in the name) not the class.
base.PropertyChanged("MyList");
You have to call INPC for the property which has been changed, i.e.
Base.OnpropertyChanged("Total");
in GridPOCO. That means GridPOCO has to implement INotifyPropertyChanged!
精彩评论