Search code examples
c#.netwpftextbox

How to clear a textBox in WPF when a clear button is clicked


I have textbox which has to be cleared when a clearfilter button is clicked. But it is not getting cleared by assigning string.Empty.

XAML file:

    <DataTemplate x:Key="FreeTextFilterTemplate">
    <StackPanel Orientation="Vertical">
        <Separator Style="{StaticResource SeparatorStyle}" Margin="5,10"/>
        <Expander Header="{Binding FilterName}" FontWeight="Bold" IsExpanded="True" Margin="10,5" Style="{StaticResource ReversedExpanderStyle}">
            <TextBox FontWeight="Normal" Margin="0,5,0,0" Text="{Binding FilterTextValue, Mode = TwoWay, UpdateSourceTrigger = PropertyChanged}" MaxLength="{Binding FilterMaxLength}" 
                    helpers:TextBoxExtension.ValidationType="{Binding TextBoxValidateType}">     
            </TextBox>
        </Expander>
    </StackPanel>
</DataTemplate>

ViewModel

    private string _filtTextValue;

    public string FilterTextValue
    {
        get => _filtTextValue;
        set
        {
            if (_filtTextValue != value)
            {
                _filtTextValue = value;
                RaisePropertyChangedEvent(FilterTextValue);
            }
        }
    }

     private void ClearSearchFilterModelData()
    {

        foreach (var filtType in SelectedCategory.Filters)
        {
            {
                if (string.Equals(InventoryFilterNameEnum.Description.ToString(), filtType.FilterName.Replace(" ", string.Empty)))
                {

                    if (!string.IsNullOrEmpty(filtType.FilterTextValue))
                    {

                        filtType.FilterTextValue = string.Empty;
                    }
                }

Other controls are getting cleared other than textbox.

     public void ButtonClickCommand(object parameter)
    {

        switch (parameter.ToString().ToLower())
        {
            case "clear_filter":
                ClearFilter();  //optical-932
                break;
            case "close":
                Close();
                break;
            case "search_filter":
                GetInventoryFilterData();
                break;
            case "printlabels": //Optical-946
                ShowPrintLabelsLegacy();
                break;
        }
    }

     private void ClearFilter()    //optical-932
    {
       SelectedCategory = Categories.FirstOrDefault(x => x.CategoryId == InventoryConstants.CATEGORY_ID_FRAMES);
        ClearFiltersOnSelectedCategory();
        ItemCount = 0;
    }

     private void ClearFiltersOnSelectedCategory()
    {
        ClearSearchFilterModelData();
    }

Control enters the if condition, goes to the property, but it does not reflect in the window.


Solution

  • I got the solution. Specifies FilterTextValue in double quotes.

        public string FilterTextValue
        {
            get => _filtTextValue;
            set
            {
                if (_filtTextValue != value)
                {
                    _filtTextValue = value; 
                    RaisePropertyChangedEvent("FilterTextValue");
                }
            }
        }