繁体   English   中英

如何在自定义控件中引发TextBox的TextChanged

[英]How to raise the TextChanged of TextBox in Custom Control

 public class CustomSearchControl : Control { static CustomSearchControl() { DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomSearchControl), new FrameworkPropertyMetadata(typeof(CustomSearchControl))); CommandManager.RegisterClassCommandBinding(typeof(CustomSearchControl), new CommandBinding(CustomSearchControl.DeleteCommand, C_DeleteCommand)); } public override void OnApplyTemplate() { base.OnApplyTemplate(); } static void C_DeleteCommand(object sender, ExecutedRoutedEventArgs e) {            CustomSearchControl mycontrol = sender as CustomSearchControl; mycontrol.SearchText = "";        } public static readonly ICommand DeleteCommand = new RoutedUICommand("DeleteCommand", "DeleteCommand", typeof(CustomSearchControl), new InputGestureCollection(new InputGesture[] { new KeyGesture(Key.Enter), new MouseGesture(MouseAction.LeftClick) })); public static readonly DependencyProperty SearchTextProperty = DependencyProperty.Register("SearchText", typeof(string), typeof(CustomSearchControl), new FrameworkPropertyMetadata( null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, SearchTextPropertyChanged)); public string SearchText { get { return (string)base.GetValue(SearchTextProperty); } set { base.SetValue(SearchTextProperty, value); } } private static void SearchTextPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) { CustomSearchControl mycontrol = d as CustomSearchControl; mycontrol.SearchText = e.NewValue.ToString(); } } 
 <ResourceDictionary x:Class="CustomSearchControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplicationCustomSearchControl"> <Style TargetType="{x:Type local:CustomSearchControl}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:CustomSearchControl}"> <Grid> <StackPanel Orientation="Horizontal"> <TextBox x:Name="tbSearchTextBox" Width="200" Height="25" Text="{TemplateBinding SearchText}"> </TextBox> <Button x:Name="btnDelete" Width="50" Height="25" Content="Delete" Command="{x:Static local:CustomSearchControl.DeleteCommand}"> </Button> </StackPanel> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary> <Window...> <Grid> <local:CustomSearchControl SearchText="{Binding Path=SearchText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/> </Grid> </Window> 

我想创建一个带有文本框和一个清除文本框的按钮的自定义控件。 如果TextBox中的Text更改,则不会引发PropertyCallBack。 引发DeleteCommand时,出现相同的问题。

怎么了?

我不太了解您要实现的目标,但我相信至少有一个错误:您为什么要使用SearchTextPropertyChanged?

您可以替换:

new FrameworkPropertyMetadata(null,FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,SearchTextPropertyChanged));

通过

new FrameworkPropertyMetadata(default(string));

就是这样,searchText能够自我更新。

无论如何,我建议您使用更经典的方法,即使用MVVM创建视图和视图模型,这将更易于实现,测试和维护。

好的,我已经解决了问题并解决了问题。 出现此问题的原因是Generic.xaml的TextBox中的“ TemplatedBinding”。

因此,首先对Generic.xaml进行更改:

 <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent},Path=SearchText,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"> 

其次是UserControl将Searchtext-Property绑定到ViewModel-Property:

 <local:CustomSearchControl SearchText="{Binding Path=ViewModelPropertyText,Mode=TwoWay}"/> 

现在它可以正常工作了,您可以将CustomControl的SearchText-Property绑定到所需的任何内容!

感谢所有考虑过我的问题的人。

暂无
暂无

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

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