如何在文本属性更改时触发 XAML 元素中的方法

How to trigger a method in a XAML element when a textproperty changes

我真的不知道如何搜索才能找到解决这个问题的方法(我用谷歌搜索了很多,也许我眼瞎了...)。

我有一个组合框,其中还包含一个文本框。 ComboBox 在具有特定 DataContext 的单独 Control.xaml 中实例化,它从中获取 Popup 列表的内容。 现在,当我在 TextBox 中键入内容时,我想触发一个方法,该方法然后针对特定元素过滤我的 DataContext 列表。 所以我的 ComboBox.cs 有以下一些内容:

    public event EventHandler FilterTextChanged;

    protected virtual void OnFilterTextChanged(EventArgs args)
    {
      FilterTextChanged?.Invoke(FilterText, args);
    }

    public string FilterText
    {
      get { return _filterText; }
      set
      {
        //This point is reached when I type something into the TextBox within the ComboBox
        if (_filterText == value) return;
        _filterText = value;
        OnFilterTextChanged(EventArgs.Empty);
        OnPropertyChanged("FilterText");
      }
    }

而在我的 Control.xaml 中,我是这样配置的:

<my:ComboBox x:Name="FURecipeComboBox"
             AuthorizationMode="IsEnabled"
             IsTextSearchEnabled="False"
             StaysOpenOnEdit="True"
             FilterTextChanged="FURecipeComboBox_OnFilterTextChanged"
             ItemsSource="{Binding RecipeFileNames}"
             SelectedItem="{Binding Value, Delay=100, ElementName=AlphaNumericTouchpadTextVarIn}"
             d:DataContext="{d:DesignInstance {x:Type adapter:ToolRecipeVariableInfo}, IsDesignTimeCreatable=False}">
</my:ComboBox>

但我无法让它捕捉到事件 "FilterTextChanged",而且我的方法 "FURecipeComboBox_OnFilterTextChanged" 将无法随时到达... 我真的很高兴能得到一些提示或帮助!

亲切的问候 BB

microsoft docs

查看 RoutedEvents

这是来自 Whosebug

的示例 post

在你的情况下,尝试将 EventHandler 更改为 RoutedEventHandler。

我做了一个小例子:

Main.xaml

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid>
    <local:UserControl1  HorizontalAlignment="Left" Height="207" Margin="348,175,0,0" VerticalAlignment="Top" Width="311" MyClick="UserControl1_MyClick"/>

</Grid>

Main.cs

using System.Windows;


namespace WpfApp1
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void UserControl1_MyClick(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Yep");
        }
    }
}

control.cs

public partial class UserControl1 : UserControl
{
    public event RoutedEventHandler MyClick;

    public UserControl1()
    {
        InitializeComponent();
    }

    private void textBox_KeyDown(object sender, KeyEventArgs e)
    {
        if(e.Key == Key.Enter)
        {
            if (MyClick != null)
                MyClick(this, new RoutedEventArgs());
        }
    }
}

control.xaml

<UserControl x:Class="WpfApp1.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:WpfApp1"
         mc:Ignorable="d" 
         d:DesignHeight="372.313" d:DesignWidth="350">
<Grid>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="218" Margin="59,54,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="202" KeyDown="textBox_KeyDown"/>

</Grid>