Home > OS >  How to clear textbox in WPF MVVM with clear button
How to clear textbox in WPF MVVM with clear button

Time:07-15

Okay, so I wanna clear the text in my textbox with a click from a button. Both are placed within the first stackpanel in the XAML code. I'm trying to make it work with commands and bindings, but I just can't seem to make it work.. Any suggestions? SqlQueryCommand is bound to the clear button, SqlQueryString is bound to the textbox.

Vievmodel:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using WpfMVVM.Commands;

namespace PoC.ViewModels
{
    class MainWindowViewModel
{
    public RelayCommand ClearSqlQueryCommand { get; private set; }
    public RelayCommand ClearFilterCommand { get; private set; }
    public RelayCommand ClearFilterByIdCommand { get; private set; }

    public event PropertyChangedEventHandler PropertyChanged;
    private string sqlQueryString;
    private string filterString;
    private string filterByIdString;
    public string SqlQueryString
    {
        get
        {
            return sqlQueryString;
        }
        set
        {
            sqlQueryString = value;
            OnPropertyChanged(nameof(SqlQueryString));
        }
    }

    public string FilterString
    {
        get
        {
            return filterString;
        }
        set
        {
            filterString = value;
            OnPropertyChanged(nameof(FilterString));
        }
    }

    public string FilterByIdString
    {
        get
        {
            return filterByIdString;
        }
        set
        {
            filterByIdString = value;
            OnPropertyChanged(nameof(FilterByIdString));
        }
    }

    public MainWindowViewModel()
    {
        ClearSqlQueryCommand = new RelayCommand(ClearSqlQuery, CanClearSqlQuery);
        ClearFilterCommand = new RelayCommand(ClearFilter, CanClearFilter);
        ClearFilterByIdCommand = new RelayCommand(ClearFilterById, CanClearFilterById);
    }

    protected void OnPropertyChanged([CallerMemberName] string name = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }

    public void ClearSqlQuery(object parameter)
    {
        SqlQueryString = string.Empty;
    }

    public bool CanClearSqlQuery(object parameter)
    {
        return true;
    }

    public void ClearFilter(object parameter)
    {

    }

    public bool CanClearFilter(object parameter)
    {
        return true;
    }

    public void ClearFilterById(object parameter)
    {

    }

    public bool CanClearFilterById(object parameter)
    {
        return true;
    }
}
}

RelayCommand:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;

namespace WpfMVVM.Commands
{
 public class RelayCommand : ICommand
   {
    Action<object> _execute;
    Func<object, bool> _canExecute;


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

    public bool CanExecute(object parameter)
    {
        if (_canExecute != null)
        {
            return _canExecute(parameter);
        }
        else
        {
            return false;
        }
    }

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

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

XAML:

<Window x:Class="PoC.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:PoC"
    xmlns:viewModels="clr-namespace:PoC.ViewModels"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
    <viewModels:MainWindowViewModel/>
</Window.DataContext>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="3*"/>
    </Grid.ColumnDefinitions>
    <TreeView>
    </TreeView>
    
    <Grid Grid.Column="1">
        <Grid.RowDefinitions>
            <RowDefinition Height="2*" />
            <RowDefinition Height="1*"/>
            <RowDefinition Height="5*"/>
        </Grid.RowDefinitions>
        <Border Grid.Row="0" BorderThickness="0 0 0 1" BorderBrush="Black"/>
        <StackPanel Grid.Row="0" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
            <TextBox Text="{Binding SqlQueryString, UpdateSourceTrigger=PropertyChanged}" Height="30" Width="450" TextAlignment="Center"/>
            <Button Content="Execute" Margin="10 0" Padding="5 0"/>
            <Button Content="Clear" Padding="5 0" Command="{Binding ClearSqlQueryCommand}"/>
        </StackPanel>
        <StackPanel Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
            <TextBox Text="{Binding FilterString}" Grid.Row="0" Height="30" Width="200"/>
            <Button Content="Filter" Margin="10 0" Padding="5 0"/>
            <Button Content="Clear" Padding="5 0" Margin="0 0 115 0"/>
            <TextBox Text="{Binding FilterByIdString}" Grid.Row="0" Height="30" Width="100"/>
            <Button Content="Filter" Margin="10 0 0 0" Padding="5 0"/>
        </StackPanel>
        <DataGrid Grid.Row="2" VerticalScrollBarVisibility="Visible" BorderThickness="0 1 1 1"/>
    </Grid>
    
    
</Grid>

CodePudding user response:

You seem to be missing : INotifyPropertyChangedon the MainWindowViewModelclass.

CodePudding user response:

You can easily do that by setting its value to "".

texbox.Text="";

  • Related