C# 向自定义控件添加传统事件

C# 向自定义控件添加传统事件,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,我一直试图通过谷歌搜索来解决这个问题,但不幸的是,我无法解决这个问题。我读得越多,就越糊涂 我想构建一个自动完成文本框作为自定义控件 我的自定义控件: <UserControl x:Class="ApplicationStyling.Controls.AutoCompleteTextBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

我一直试图通过谷歌搜索来解决这个问题,但不幸的是,我无法解决这个问题。我读得越多,就越糊涂

我想构建一个自动完成文本框作为自定义控件

我的自定义控件:

<UserControl x:Class="ApplicationStyling.Controls.AutoCompleteTextBox"
             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:ApplicationStyling.Controls"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="300"
             Name="AutoCompleteBox">
    <Grid>
        <TextBox Grid.Row="3"
                 Style="{DynamicResource InputBox}"
                 x:Name="SearchBox"
                 Text="{Binding Text}"
                 TextChanged="{Binding ElementName=AutoCompleteBox, Path=TextChanged}"/>

        <ListBox x:Name="SuggestionList"
                 Visibility="Collapsed"
                 ItemsSource="{Binding ElementName=AutoCompleteTextBox, Path=SuggestionsSource}"
                 SelectionChanged="{Binding ElementName=AutoCompleteBox, Path=SelectionChanged}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Label Content="{Binding Label}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>

我的代码隐藏:

using System.Collections;
using System.Windows;
using System.Windows.Controls;

namespace ApplicationStyling.Controls
{
    /// <summary>
    /// Interaction logic for AutoCompleteTextBox.xaml
    /// </summary>
    public partial class AutoCompleteTextBox : UserControl
    {
           
        public static readonly DependencyProperty SuggestionsSourceProperty;
        public static readonly DependencyProperty TextProperty;

        // Events
        public static readonly RoutedEvent TextChangedProperty;
        public static readonly RoutedEvent SelectionChangedProperty;


        static AutoCompleteTextBox()
        {
            // Attributes
            AutoCompleteTextBox.SuggestionsSourceProperty = DependencyProperty.Register("SuggestionsSource", typeof(IEnumerable), typeof(AutoCompleteTextBox));
            AutoCompleteTextBox.TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(AutoCompleteTextBox));

            // Events
            AutoCompleteTextBox.TextChangedProperty = EventManager.RegisterRoutedEvent("TextChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AutoCompleteTextBox));
            AutoCompleteTextBox.SelectionChangedProperty = EventManager.RegisterRoutedEvent("SelectionChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AutoCompleteTextBox));

        }

        #region Events
        public event RoutedEventHandler TextChanged
        {
            add { AddHandler(TextChangedProperty, value); }
            remove { RemoveHandler(TextChangedProperty, value); }
        }

        // This method raises the Tap event
        void RaiseTextChangedEvent()
        {
            RoutedEventArgs newEventArgs = new RoutedEventArgs(AutoCompleteTextBox.TextChangedProperty);
            RaiseEvent(newEventArgs);
        }



        public event RoutedEventHandler SelectionChanged
        {
            add { AddHandler(SelectionChangedProperty, value); }
            remove { RemoveHandler(SelectionChangedProperty, value); }
        }

        // This method raises the Tap event
        void RaiseSelectionChangedEvent()
        {
            RoutedEventArgs newEventArgs = new RoutedEventArgs(AutoCompleteTextBox.SelectionChangedProperty);
            RaiseEvent(newEventArgs);
        }

        #endregion

        #region DProperties
        /// <summary>
        /// IEnumerable ItemsSource Property for the Suggenstion Box
        /// </summary>
        public IEnumerable SuggestionsSource
        {
            get
            {
                return (IEnumerable)GetValue(AutoCompleteTextBox.SuggestionsSourceProperty);
            }
            set
            {
                SetValue(AutoCompleteTextBox.SuggestionsSourceProperty, value);
            }
        }

        /// <summary>
        /// This is the Text attribute which routes to the Textbox
        /// </summary>
        public string Text
        {
            get
            {
                return (string)GetValue(AutoCompleteTextBox.TextProperty);
            }
            set
            {
                SetValue(AutoCompleteTextBox.TextProperty, value);
            }
        }
        #endregion

        public AutoCompleteTextBox()
        {
            InitializeComponent();
            SearchBox.TextChanged += (sender, args) => RaiseTextChangedEvent();
            SuggestionList.SelectionChanged += (sender, args) => RaiseSelectionChangedEvent();
        }


    }
}
使用系统集合;
使用System.Windows;
使用System.Windows.Controls;
命名空间应用程序类型化.Controls
{
/// 
///AutoCompleteTextBox.xaml的交互逻辑
/// 
公共部分类AutoCompleteTextBox:UserControl
{
公共静态只读DependencyProperty SuggestionSourceProperty;
公共静态只读DependencyProperty TextProperty;
//事件
公共静态只读路由事件文本更改属性;
公共静态只读路由事件选择更改属性;
静态自动完成文本框()
{
//属性
AutoCompleteTextBox.SuggestionSourceProperty=DependencyProperty.Register(“SuggestionSource”、typeof(IEnumerable)、typeof(AutoCompleteTextBox));
AutoCompleteTextBox.TextProperty=dependencProperty.Register(“Text”、typeof(string)、typeof(AutoCompleteTextBox));
//事件
AutoCompleteTextBox.TextChangedProperty=EventManager.RegisterRoutedEvent(“TextChanged”,RoutingStrategy.Bubble,typeof(RoutedEventHandler),typeof(AutoCompleteTextBox));
AutoCompleteTextBox.SelectionChangedProperty=EventManager.RegisterRoutedEvent(“SelectionChanged”,RoutingStrategy.Bubble,typeof(RoutedEventHandler),typeof(AutoCompleteTextBox));
}
#地区活动
公共事件路由EventHandler文本已更改
{
添加{AddHandler(TextChangedProperty,value);}
删除{RemoveHandler(TextChangedProperty,value);}
}
//此方法引发Tap事件
void RaiseTextChangedEvent()
{
RoutedEventArgs newEventArgs=新RoutedEventArgs(AutoCompleteTextBox.TextChangedProperty);
RaiseEvent(newEventArgs);
}
公共事件路由EventHandler选择已更改
{
添加{AddHandler(SelectionChangedProperty,value);}
删除{RemoveHandler(SelectionChangedProperty,value);}
}
//此方法引发Tap事件
void RaiseSelectionChangedEvent()
{
RoutedEventArgs newEventArgs=新RoutedEventArgs(AutoCompleteTextBox.SelectionChangedProperty);
RaiseEvent(newEventArgs);
}
#端区
#区域性房地产
/// 
///建议框的IEnumerable ItemsSource属性
/// 
公共IEnumerable SuggestionSource
{
得到
{
返回(IEnumerable)GetValue(AutoCompleteTextBox.SuggestionSourceProperty);
}
设置
{
SetValue(AutoCompleteTextBox.SuggestionSourceProperty,值);
}
}
/// 
///这是路由到文本框的文本属性
/// 
公共字符串文本
{
得到
{
返回(字符串)GetValue(AutoCompleteTextBox.TextProperty);
}
设置
{
SetValue(AutoCompleteTextBox.TextProperty,值);
}
}
#端区
公共自动完成文本框()
{
初始化组件();
SearchBox.TextChanged+=(发件人,参数)=>RaiseTextChangedEvent();
SuggestionList.SelectionChanged+=(发件人,参数)=>RaiseSelectionChangedEvent();
}
}
}
最后,我使用它的方式:

<asc:AutoCompleteTextBox x:Name="ShareAutoCompleteBox"
                                 Grid.Row="3"
                                 SelectionChanged="ShareAutoCompleteBox_SelectionChanged"
                                 TextChanged="ShareAutoCompleteBox_TextChanged"/>
(用于事件)和其他一些SO询问自定义属性的代码


不确定问题是什么,我期待您的帮助。

发生异常是因为您试图将
TextChanged
字段的值绑定到需要方法引用的属性。这对WPF来说真的很困惑。:)

只需从XAML中的
TextBox
元素中删除
TextChanged
属性:

您已经在构造函数中订阅了事件,这就足够了。如果您确实希望使用
TextChanged
属性而不是在构造函数中订阅,那么您可以这样做,但您需要提供实际的事件处理程序,例如代码隐藏中的方法。该方法只需调用
RaiseTextChangedEvent()
方法,就像当前事件处理程序一样。只是它将是类中的命名方法,而不是构造函数中声明的匿名方法

同样的事情也适用于其他事件


也就是说,您可能会重新考虑实现转发的事件。通常,控件的
Text
属性将绑定到某个模型对象的属性,当绑定属性更改时,该模型对象本身可以做出适当的反应。它不需要在
UserControl
对象上有单独的事件来告诉它它的值已更改

TextChanged="{Binding ElementName=AutoCompleteBox, Path=TextChanged}"