Tlačítko Obsah neaktualizuje [MVVM]

0

Otázka

V mém Kódu se moje Funkce OnPropertyChanged aktualizace UI, a to navzdory Skutečnosti, že to funguje všude jinde v mém Programu. Snažím se aktualizovat Obsah Tlačítko, tak, že pokud Uživatel stiskne, změní se jeho Obsah. To Aktualizovat value ale pak se nebude zobrazovat v ROZHRANÍ pro nějakého podivného Důvodu. Každá pomoc je vítána... Zobrazení:

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

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

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

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>
button c# mvvm user-interface
2021-11-23 14:10:24
1

Nejlepší odpověď

3

Na CreateMPViewModel třída musí dědit od INotifyPropertyChanged.

public class CreateMPViewModel : Window, INotifyPropertyChanged
{
    //........................................
    //........................................
}
2021-11-23 14:57:03

Díky, nebyl jsem si vědom, že! :)
Deto24

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................