Home > Back-end >  Button Content doesn't Update [MVVM]
Button Content doesn't Update [MVVM]

Time:11-24

In my Code does my Function OnPropertyChanged not update the UI, despite the Fact that it does work everywhere else in my Programm. I try to upgrade the Content of the Button, so that if the User presses it, it will change it's Content. It does Update the value but then won't show it in the UI for some weird Reason. Any help is appreciated... The View:

public partial class CreateMP : Window
{
    public CreateMP()
    {
        InitializeComponent();
    }

    public CreateMP(string UserName)
    {
        CreateMPViewModel.User = UserName;
        InitializeComponent();
    }
}

The ViewModel:

public class CreateMPViewModel : Window
{
    private string _ButtonContent = "Create Server";
    private bool _ButtonEnable = true;

    public event PropertyChangedEventHandler PropertyChanged;

    private readonly TcpListener listener;
    private TcpClient client;

    public ICommand ButtonCommand_Back { get; set; }
    public ICommand ButtonCommand_CreateGame { get; set; }

    public string ButtonContent
    {
        get
        {
            return _ButtonContent;
        }
        set
        {
            if (value != _ButtonContent)
            {
                _ButtonContent = value;
                OnPropertyChanged("ButtonContent");
            }
        }
    }

    public bool ButtonEnable
    {
        get
        {
            return _ButtonEnable;
        }
        set
        {
            if (value != _ButtonEnable)
            {
                _ButtonEnable = value;
                OnPropertyChanged("ButtonEnable");
            }
        }
    }

    public static string User { get; set; }
    public string IPAdresse { get; set; }
    public int Passwort { get; set; }

    public CreateMPViewModel()
    {
        ButtonCommand_Back = new DelegateCommand(BackButtonClick);
        ButtonCommand_CreateGame = new DelegateCommand(CreateGameClick);

        int Port = 51246;
        Random rnd = new Random();
        Password = rnd.Next(0, 99999);
        IPAddress localAdd = IPAddress.Parse(IPAdresse);
        listener = new TcpListener(localAdd, Port);

        listener.Start();
    }

    private void BackButtonClick()
    {
        client = GameHandler.CurrentClient;
        Application.Current.MainWindow.Close();

        if (client != null)
        {
            client.Close();
        }
        if (listener != null)
        {
            listener.Stop();
        }
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private void CreateGameClick()
    {
        ButtonEnable = false;
        ButtonContent = "Waiting for Player...";
        //More Code here after the Update of the Button
    }
}

The XAML:

<Window x:Class="CreateMP"
        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"
        mc:Ignorable="d"
        Title="Game" Height="450" Width="400"
        WindowStartupLocation="CenterScreen"
        ResizeMode="NoResize"  WindowStyle="None">
    <Window.DataContext>
        <local1:CreateMPViewModel/>
    </Window.DataContext>

    <Grid>
        <Label Name ="CreateGame" Content="Create Game" Margin="109,46,118,322" FontSize="26" Foreground="#0074BC"/>
        <Button Name ="btnCreateGame" Content="{Binding ButtonContent, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="35" Margin="118,267,118,0" VerticalAlignment="Top" Background="#0074BC" Foreground="White" Command="{Binding ButtonCommand_CreateGame}" IsEnabled="{Binding ButtonEnable, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        <Button Name ="btnBack" Content="Back" Height="35" Margin="118,387,118,0" VerticalAlignment="Top" Background="#0074BC" Foreground="White" Command="{Binding ButtonCommand_Back}"/>
        <Label Content="IP4-Adress:" HorizontalAlignment="Left" Height="30" Margin="92,138,0,0" VerticalAlignment="Top" Width="90" Foreground="#0074BC"/>
        <Label Content="Password:" HorizontalAlignment="Left" Height="30" Margin="92,168,0,0" VerticalAlignment="Top" Width="90" Foreground="#0074BC"/>
        <Label Content="{Binding IPAdresse}" HorizontalAlignment="Left" Height="30" Margin="187,138,0,0" VerticalAlignment="Top" Width="127" Foreground="#0074BC"/>
        <Label Content="{Binding Password}" HorizontalAlignment="Left" Height="30" Margin="187,168,0,0" VerticalAlignment="Top" Width="127" Foreground="#0074BC"/>
    </Grid>
</Window>

CodePudding user response:

The CreateMPViewModel class must inherit from INotifyPropertyChanged.

public class CreateMPViewModel : Window, INotifyPropertyChanged
{
    //........................................
    //........................................
}
  • Related