G
G
Georgy Kuznetsov2021-07-18 20:36:14
C++ / C#
Georgy Kuznetsov, 2021-07-18 20:36:14

How to change content in textboxes through bindings?

Is there any way in this case to change the content in the textbox on the click of a button?

Here's what I tried to do myself: let's say there are several files:
+ProjectFolder
-- MainWindow.xaml
-- MainWindow.xaml.cs (only the mainWindow constructor inside which the component initialization method)
-- TCommand.cs
-- MainWindowVM.cs

+++ MainWindow.xaml +++

<Window x:Class="Player.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:Player"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Title="MainWindow" ResizeMode="NoResize" Height="840" Width="1420">
    
    <Window.DataContext>
        <local:MainWindowVM/>
    </Window.DataContext>

    <Grid>
         <TextBox Text="{Binding PValue_1}"  HorizontalAlignment="Left" Height="35" TextWrapping="Wrap" VerticalAlignment="Top" Width="261" Margin="1075,229,0,0"/>
        <Button Command="{Binding OnClick_1}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="35" Margin="1349,229,0,0" Height="35"/>
    </Grid>


+++ MainWindow.xaml.cs +++

    class MainWindowVM 
    {

        public event PropertyChangedEventHandler PropertyChanged;

        public void CheckChanges([CallerMemberName]string property="")
        {
            PropertyChanged?.Invoke(PropertyChanged, new PropertyChangedEventArgs(property));
        }

        public string value_1 = "DROP";

        public string PValue_1
        {
            get
            {
                return value_1;
            }

            set
            {
                value_1 = value;
                CheckChanges();
            }
        }

        public TCommand OnClick_1
        {
            get 
            {
                return new TCommand
                (
                    (obj) =>
                    {
                        PValue_1 = "kurwa";
                    }
                );
            }
        }
    }


+++ TCommand.cs +++

class TCommand
    {
        private Action<object> execute;
        private Func<object, bool> canExecute;

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

        public TCommand(Action<object> _execute, Func<object, bool> _canExecute = null)
        {
            execute = _execute;
            canExecute = _canExecute;
        }

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

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


Question: why doesn't this work?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question