Hallo
Ich bin dabei dieses MVVM zu vertiefen, nur habe ich ein Problem, wenn ich einen String ändere dann bekommt mein Label nicht automatisch den neuen Inhalt.
Anbei sende ich mein Code, irgendwas habe ich da Falsch.
ViewModelBase.cs
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Variable_Test
{
internal class ViewModelBase
{
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (object.Equals(storage, value)) return false;
storage = value;
// Log.DebugFormat("{0}.{1} = {2}", this.GetType().Name, propertyName, storage);
this.OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
#endregion INotifyPropertyChanged implementation
}
}
Data.cs
namespace Variable_Test
{
internal class Data : ViewModelBase
{
private string _Text1;
public string Text1
{
get => _Text1;
set => SetProperty(ref _Text1, value);
}
}
}
DataRead.cs
namespace Variable_Test
{
internal class DataRead : Data
{
public void Inhalt()
{
Text1 = "Hallo";
}
public void myText1(string Value)
{
Text1 = Value;
}
}
}
MainWindow.xaml.cs
using System.Windows;
namespace Variable_Test
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DataRead myDate = new DataRead();
public MainWindow()
{
InitializeComponent();
myDate.Inhalt();
this.DataContext = myDate;
}
private void B_Button1_Click(object sender, RoutedEventArgs e)
{
myDate.myText1("Hallo Button");
MessageBox.Show(myDate.Text1);
}
}
}
MainWindow.xaml
<Window x:Class="Variable_Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Variable_Test"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button x:Name="B_Button1" Content="Button" Click="B_Button1_Click" HorizontalAlignment="Left" Margin="370,74,0,0" VerticalAlignment="Top"/>
<Label Content="{Binding Text1}" HorizontalAlignment="Left" Margin="81,84,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>
Was habe ich hier falsch?
oder was muss ich tun das der Inhalt in Label Content sich Automatisch ändert.
Gruß
Mezzo
Dein ViewModel muss das INotifyPropertyChanged-Interface implementieren.
Siehe dazu: https://mycsharp.de/forum/threads/118261/artikel-mvvm-und-databinding
Weeks of programming can save you hours of planning
Hallo
Danke für die Antwort, diese habe ich heute Vormittag auch schon herausgefunden, hatte aber erst jetzt Zeit um zu Antworten. mein ViewModelBase hat dieses gefehlt.
hier der Richtige Code von
ViewModelBase.cs
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Variable_Test
{
internal class ViewModelBase : INotifyPropertyChanged
{
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (object.Equals(storage, value)) return false;
storage = value;
// Log.DebugFormat("{0}.{1} = {2}", this.GetType().Name, propertyName, storage);
this.OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
#endregion INotifyPropertyChanged implementation
}
}