如何通过单击按钮从文本框中删除过滤器文本

How to delete filter text from textbox with buttonclick

我正在尝试从文本框中删除过滤器文本,但到目前为止我无法执行此操作。我有一个工作正常的 collectionview 过滤器。当我添加一个按钮来清除过滤器文本框中的过滤器和文本时,集合过滤器被清除并显示完整集合,但旧过滤器的文本仍然显示。我在这个项目中使用棱镜。

这是过滤器和清除过滤器的代码部分。

    class ScripterViewModel : BindableBase
{
    public ScripterViewModel()
    {
        ScripterModel scripterModel = new ScripterModel();
        KeysToChoose = new ObservableCollection<Keys>();



        this.AddItemBtn = new DelegateCommand<Keys>(addItem);
        this.AddTextBtn = new DelegateCommand(addText);
        this.ClearSearchBtn = new DelegateCommand(ClearSearch);

        SelectedOption = "Lathe Keys";


        ItemsView.Filter = new Predicate<object>(o => Filter(o as Keys));
    }

    private bool Filter(Keys keys)
    {
        return Search == null
            || keys.Description.IndexOf(Search, StringComparison.OrdinalIgnoreCase) != -1;
    }

    public ICollectionView ItemsView
    {
        get
        {
            return CollectionViewSource.GetDefaultView(KeysToChoose);
        }
    }            
    
    private string _search;

    public string Search
    {
        get 
        { 
            return _search; 
        }
        set
        {
            _search = value;
            SetProperty(ref _search, value);
            ItemsView.Refresh();
        }
    }
    public event EventHandler CanExecuteChanged
    {
        add
        {
            CommandManager.RequerySuggested += value;
        }
        remove
        {
            CommandManager.RequerySuggested -= value;
        }
    }
    public bool CanClearSearch()
    {
        return true;
    }
    public ICommand ClearSearchBtn
    {
        get;
        set;
    }

    public void ClearSearch()
    {
        if (Search != null)
        {
            try
            {
                Search = "";
            }
            catch (Exception e)
            {
                Console.WriteLine("error: '{0}'", e);
            }

        }
    }

添加一些 xaml 代码 文本框名称 SearchText 是我想通过单击按钮清除的内容。

            </TabControl>
        <TextBox BorderBrush="Black" HorizontalAlignment="Left" Margin="48,19,0,0" TextWrapping="Wrap" Text="{Binding Path=ClipBoardText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="502" Height="25" IsEnabled="False" />
        <TextBox x:Name="SearchText" HorizontalAlignment="Left" Height="23" Margin="48,75,0,0" TextWrapping="Wrap" Text="{Binding Path=Search, Mode=TwoWay,  UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="502" RenderTransformOrigin="0.496,1.085"/>
        <Button x:Name="ClearSearchBtn" Content="Clear Search" Command="{Binding Path=ClearSearchBtn}" HorizontalAlignment="Left" Margin="48,49,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.371,-0.704" Visibility="Visible"/>
    </Grid>
  • ClearSearchBtn 不起作用,因为您的搜索没有触发 PropertyChanged。
  • SetProperty(ref _search, value) :内部工作逻辑是PropertyChanged只有在value改变时才会触发

搜索重复设定值

private string _search;

public string Search
{
    get 
    { 
        return _search; 
    }
    set
    {
        _search = value;// remove this
        SetProperty(ref _search, value);
        ItemsView.Refresh();
    }
}