单击清除按钮时如何在WPF中清除文本框

时间:2019-11-08 09:25:23

标签: c# .net wpf textbox

我有一个文本框,单击清除过滤器按钮时必须清除它。但是不能通过分配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();
    }

控件输入if条件,转到属性,但是它不反映在窗口中。

2 个答案:

答案 0 :(得分:0)

确保在RaisePropertyChangedEvent中传递属性的名称,而不是内容。

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

答案 1 :(得分:0)

我找到了解决方案。用双引号指定FilterTextValue。

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