按钮内容不更新 [MVVM]

Posted

技术标签:

【中文标题】按钮内容不更新 [MVVM]【英文标题】:Button Content doesn't Update [MVVM] 【发布时间】:2022-01-02 00:49:57 【问题描述】:

在我的代码中,我的函数 OnPropertyChanged 不会更新 UI,尽管事实上它在我的 Programm 中的其他任何地方都可以工作。我尝试升级按钮的内容,这样如果用户按下它,它将改变它的内容。它确实更新了value,但由于某些奇怪的原因,它不会在 UI 中显示。任何帮助表示赞赏... 观点:

public partial class CreateMP : Window

    public CreateMP()
    
        InitializeComponent();
    

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

视图模型:

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>

【问题讨论】:

顺便说一句,您的 CreateMPViewModel 不应继承自 Window。在这种情况下,它不是 ViewModel,而是 View -> 这不是一个好的 MVVM 示例。 :) 【参考方案1】:

CreateMPViewModel 类必须从INotifyPropertyChanged继承

public class CreateMPViewModel : Window, INotifyPropertyChanged

    //........................................
    //........................................

【讨论】:

谢谢,我不知道! :)

以上是关于按钮内容不更新 [MVVM]的主要内容,如果未能解决你的问题,请参考以下文章

ajax调用后kendo mvvm不更新

如何在 MVVM-WPF 中获取所选项目

为啥 ListView 在 MVVM 中不更新 [重复]

当 ObservableCollection 更改时 ItemsControl 不更新

[DataGridCheckBoxColumn在属性更改时不会在MVVM中更新

抽象 ViewModel 不更新 LiveData Android MVVM