如何实现INotifyPropertyChange(WPF,MVVM)

时间:2018-04-18 10:22:14

标签: c# wpf mvvm inotifypropertychanged

当我在我的ViewModel中设置默认值时,它可以工作,但是当我更改任何内容时,View保持不变....所以我认为这与INotifyPropertyChanged有关。 我花了大约10个小时“googleing”,但我找不到什么错。 所以这是我的代码,我希望你能告诉我什么是错的:)

我的观点:

<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();
    } 
}

我的ViewModel:

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

3 个答案:

答案 0 :(得分:-1)

仅仅是为了获取信息,我试图将这种模式与一些辅助属性结合使用。

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));
    }
 }

它应该有用。

...或者因为它被投票赞成:也许不是。然而。尝试使用帮助器。它可以保护您免受非强类型拼写错误的侵害。

答案 1 :(得分:-1)

您在TasteView.Xaml.cs中设置了无效的construtor。它应该像

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;
    }`

希望这对你有用。

答案 2 :(得分:-2)

你的导师错了:

//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()