繁体   English   中英

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

[英]How to clear a textBox in WPF when a clear button is clicked

我有文本框,单击 clearfilter 按钮时必须清除该文本框。 但它并没有通过分配 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>

视图模型

    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条件,进入属性,但在window中没有体现。

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM