簡體   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