[英]How to implement INotifyPropertyChange (WPF, MVVM)
當我在 ViewModel 中設置默認值時,它可以工作,但是當我更改任何內容時,視圖保持不變......所以我認為這與 INotifyPropertyChanged 有關。 我花了大約 10 個小時“谷歌搜索”,但我無法找出問題所在。 所以這是我的代碼,我希望你能告訴我有什么問題:)
我的觀點:
<UserControl x:Class="Diplomarbeit.Views.TasteView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Diplomarbeit"
mc:Ignorable="d"
>
<Grid>
<Ellipse x:Name="Taste" Fill="{Binding FillColor, Mode=TwoWay}" Stroke="{Binding BorderColor, Mode=TwoWay}" StrokeThickness="10" Height="{Binding ActualWidth, RelativeSource={RelativeSource Self}}" Margin="2,0,2,0"/>
<Viewbox>
<Label x:Name="TasteText" Content="{Binding Text, Mode=TwoWay}" Foreground="{Binding TextColor, Mode=TwoWay}" FontWeight="ExtraBold" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Viewbox>
</Grid>
視圖的代碼隱藏:
public partial class TasteView : UserControl
{
public TasteView()
{
InitializeComponent();
// DataContext der View auf ViewModel binden
this.DataContext = new TasteViewModel();
}
}
我的視圖模型:
public class TasteViewModel : INotifyPropertyChanged
{
#region PropertyChanged-Event + Methode
public event PropertyChangedEventHandler PropertyChanged; // Event wird deklariert
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); // Event wird gefeuert
}
}
#endregion
#region Felder
//ID für Eindeutigkeit
private int _iD;
//Füllfarbe der Ellipse
private SolidColorBrush _fillColor;
//Randfarbe der Ellipse
private SolidColorBrush _borderColor;
//Text der im Label steht; ACHTUNG: Vor und nach der Zahl 2 Leerzeichen!
private string _text;
//Farbe des Texts im Label
private SolidColorBrush _textColor;
#endregion
#region Eigenschaften
//ID für Eindeutigkeit
public int ID
{
get { return _iD; }
set
{
if (value != _iD)
{
_iD = value;
OnPropertyChanged("ID");
}
}
}
//Füllfarbe der Ellipse
public SolidColorBrush FillColor
{
get { return _fillColor; }
set
{
if (value != _fillColor)
{
_fillColor = value;
OnPropertyChanged("FillColor");
}
}
}
//Randfarbe der Ellipse
public SolidColorBrush BorderColor
{
get { return _borderColor; }
set
{
if (value != _borderColor)
{
_borderColor = value;
OnPropertyChanged("BorderColor");
}
}
}
//Text der im Label steht
public string Text
{
get { return _text; }
set
{
if (value != _text)
{
_text = value;
OnPropertyChanged("Text");
}
}
}
//Farbe des Texts im Label
public SolidColorBrush TextColor
{
get { return _textColor; }
set
{
if (value != _textColor)
{
_textColor = value;
OnPropertyChanged("TextColor");
}
}
}
#endregion
#region Konstruktoren
//Farbige Taste mit Border, Text und ID ==> Vollständige Taste
public TasteViewModel(int iD, SolidColorBrush fillColor, SolidColorBrush borderColor, string text, SolidColorBrush textColor)
{
iD = ID;
fillColor = FillColor;
borderColor = BorderColor;
text = Text;
textColor = TextColor;
}
//Farbige Taste mit Border und ID, jedoch ohne Text
public TasteViewModel(int iD, SolidColorBrush fillColor, SolidColorBrush borderColor)
{
iD = ID;
fillColor = FillColor;
borderColor = BorderColor;
}
//Leere Taste, allerdings mit ID
public TasteViewModel(int iD)
{
iD = ID;
}
//Leere Taste
public TasteViewModel()
{
}
#endregion
我將此模式與一些輔助屬性一起使用:
public class ViewModel: INotifyPropertyChanged
{
private string _text;
public string Text
{
get { return _text; }
set
{
if (value == _text) return;
_text= value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
它應該工作。
無論如何,請嘗試使用助手。 它可以保護您免受非強類型錯字的侵害。
您在 TasteView.Xaml.cs 中設置了無效的構造函數。 它應該像
public TasteView()
{
InitializeComponent();
// DataContext der View auf ViewModel binden
this.DataContext = new TasteViewModel(1,Brushes.Red,Brushes.Green,"Taste",Brushes.Blue);
}
那個構造函數應該如下所示。
public TasteViewModel(int iD, SolidColorBrush fillColor, SolidColorBrush borderColor, string text, SolidColorBrush textColor)
{
ID = iD;
FillColor = fillColor;
BorderColor = borderColor;
Text = text;
TextColor = textColor;
}`
希望這對你有用。
您的構造函數是錯誤的:
//Farbige Taste mit Border, Text und ID ==> Vollständige Taste
public TasteViewModel(int iD, SolidColorBrush fillColor, SolidColorBrush borderColor, string text, SolidColorBrush textColor)
{
ID = iD;
FillColor = fillColor ;
BorderColor = borderColor;
Text = text;
TextColor = textColor;
}
其他兩個也一樣。
編輯:雙向綁定是默認行為,無需在 xaml 中明確說明。
如果您使用的是 c# 6 或更新版本,您應該使用nameof()
運算符進行 OnPropertyChanged OnPropertyChanged()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.