Command in Button not reacting

31 Views Asked by At

I'm new in C# and I was just repeating the code in video, but I got stuck in the moment where in video the button is connecting you to local host. I have absolutely no clue why the line Command="{Binding ConnectToServerCommand}" not working. Also I added some "checks" to check if it's at least reacting. Seems i did wrong somewhere or just didn't bind correctly. Can someone explain why it's not working please?

<Window x:Class="Chat.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:Chat" xmlns:viewmodel="clr-namespace:Chat.MVVM.ViewModel"
        mc:Ignorable="d"
        Title="MainWindow" 
        Height="450"
        Width="800">

    <Window.DataContext>
        <viewmodel:MainViewModel/>
    </Window.DataContext>
    
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <DockPanel>
            <TextBox Height="25"
                 DockPanel.Dock="Top"/>
            <Button Height="25"
                DockPanel.Dock="Top"
                Content="Connect"
                Command="{Binding ConnectToServerCommand}"/>
            <ListView/>
            <TextBlock Text="{Binding IsConnectCommandExecuted, StringFormat='Connect command executed: {0}'}" />
        </DockPanel>

        <StackPanel Grid.Column="1">
            <ListBox Height="380"/>
            <StackPanel Orientation="Horizontal">
                <TextBox Height="55"
                         Width="545"
                         VerticalContentAlignment="Center"/>
                <Button Width="55"
                        Content="Send"/>
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>

MainModel:

using Chat.MVVM.Core;
using Chat.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chat.MVVM.ViewModel
{
    class MainViewModel
    {
        private bool _isConnectCommandExecuted;
        public bool IsConnectCommandExecuted
        {
            get { return _isConnectCommandExecuted; }
            set
            {
                _isConnectCommandExecuted = value;
                OnPropertyChanged(nameof(IsConnectCommandExecuted));
            }
        }
        public RelayCommand ConnectToServerCommand { get; set; }
        public Server _server;
        public MainViewModel()
        {
            _server = new Server();
            ConnectToServerCommand = new RelayCommand(o => _server.ConnectToServer());
        }


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

RelayCommand:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Chat.MVVM.Core
{
    class RelayCommand
    {
        private Action<object> execute;
        private Func<object, bool> canExecute;

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            return this.canExecute == null || this.canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            this.execute(parameter);
        }
    }
}

There the code should connect you to localhost and throw an exception but button is just not reacting

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Chat.Net
{
    class Server
    {
        TcpClient _client;
        public Server()
        {
            _client = new TcpClient();
        }
        public void ConnectToServer()
        {
            if (!_client.Connected)
            {
                _client.Connect("127.0.0.1", 2578);
                IsConnectCommandExecuted = true;
            }
        }

        public bool IsConnectCommandExecuted { get; set; }
    }
}
0

There are 0 best solutions below