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

时间:2017-05-12 08:31:25

标签: c# wpf

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和Button的自定义控件来清除TextBox。 如果TextBox中的Text发生更改,则不会引发PropertyCallBack。引发DeleteCommand时会出现同样的问题。

怎么了?

2 个答案:

答案 0 :(得分:2)

我真的不明白你想要达到的目标,但我相信至少有一个错误:你为什么要处理SearchTextPropertyChanged?

您可以替换:

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

通过

new FrameworkPropertyMetadata(default(string));

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

在任何情况下,我建议您使用MVVM创建视图和视图模型来使用更经典的方法,它将更容易实现,测试和维护。

答案 1 :(得分:0)

好的,我已经找到问题并解决了问题。问题的原因是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绑定到您想要的任何内容!

感谢所有想过我的问题的人。