When i set default values in my ViewModel, it works, but when i change anything, the View stays the same....so i think this has to do with INotifyPropertyChanged. I've spent about 10 hours "googleing", but i couldn't find out whats wrong. So here's my code, i hope you can tell me what's wrong :)
My View:
<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>
Codebehind of the View:
public partial class TasteView : UserControl
{
public TasteView()
{
InitializeComponent();
// DataContext der View auf ViewModel binden
this.DataContext = new TasteViewModel();
}
}
My 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
I use this pattern with some helper attributes:
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));
}
}
It should work.
In any case, try to use the helpers. It protect you against non-strong-typed-typo's.