0
我有一個在運行時更新的viewmodel類。但是,如果視圖模型中的值在運行時的代碼隱藏中發生更改,則視圖不會更新。我哪裏做錯了?當代碼隱藏值更改時,用戶控件未更新
視圖模型看起來是這樣的:
public class vmA : modelA, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(PropertyChangedEventArgs propertyChangedEventArgs)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyChangedEventArgs.PropertyName));
}
public vmA()
{
SomeValue = 254.43F; //This value is shown when the control is loaded
}
public void SetSomeValue(int _someValue)
{
SomeValue = _someValue; // If this is executed, the view still shows 254.43, even though _someValue contains a different value
}
}
(這是由視圖模型繼承)模型類看起來像這樣
public class modelA : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs propertyChangedEventArgs)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyChangedEventArgs.PropertyName));
}
private int someValue
public int SomeValue
{
get { return someValue
set { someValue value; OnPropertyChanged(new PropertyChangedEventArgs("SomeValue")); }
}
}
綁定是這樣
<UserControl.DataContext>
<vm:vmA></vm:vmA>
</UserControl.DataContext>
完成
該文本框綁定:
<TextBox x:Name="tbBetragNetto" Text='{Binding BetragNetto, Mode=TwoWay}' Grid.Column="0"/>
謝謝。我會嘗試。接下來是否會通知視圖有關視圖模型的更改,例如,如果向視圖模型添加其他屬性? –
是的。因爲視圖模型繼承了實現'INotifyPropertyChanged'的東西,所以它自然也會有。 – Laith
謝謝!工作正常 –