Как очистить текстовое поле в WPF при нажатии кнопки очистки - PullRequest
0 голосов
/ 08 ноября 2019

У меня есть текстовое поле, которое необходимо очистить при нажатии кнопки очистки фильтра. Но это не очищается назначением string.Empty.

XAML-файл:

    <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;
                    }
                }

Другие элементы управления очищаются, кроме текстового поля.

     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();
    }

Элемент управления вводится, еслиусловие, переходит в собственность, но не отражается в окне.

Ответы [ 2 ]

0 голосов
/ 09 ноября 2019

Я получил решение. Задает FilterTextValue в двойных кавычках.

    public string FilterTextValue
    {
        get => _filtTextValue;
        set
        {
            if (_filtTextValue != value)
            {
                _filtTextValue = value; 
                RaisePropertyChangedEvent("FilterTextValue");
            }
        }
    }
0 голосов
/ 08 ноября 2019

Убедитесь, что вы передаете имя свойства в RaisePropertyChangedEvent вместо содержимого.

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