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>
I want to create a Custom Control with a TextBox and a Button that clears the TextBox. If Text in TextBox changed, the PropertyCallBack isn't raised. The same issue appears, when DeleteCommand is raised.
What's wrong?