Reputation: 1004
I have a style for a WPF text box which looks some thing like this. The template has a button on whose button clicked event i need the text to be set to empty. Can someone tell what am i missing. Or how can I call a method inside the custom SearchTextBox say ( OnButtonClikced) custom method
<Style TargetType="{x:Type DevExGrdDemo:SearchTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DevExGrdDemo:SearchTextBox}">
<Border x:Name="Border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid x:Name="LayoutGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualHeight}" />
</Grid.ColumnDefinitions>
<ScrollViewer x:Name="PART_ContentHost"
Grid.Column="0"
Margin="2" />
<Label x:Name="LabelText"
Grid.Column="0"
Margin="2"
Content="Search"
FontStyle="Italic"
Padding="2,0,0,0" />
<Button x:Name="SearchBoxButton"
Grid.Column="1"
Style="{DynamicResource SearchButton}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click" >
<"A method residing in the SearchTextBox called on button click">
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="HasText" Value="True">
<Setter TargetName="LabelText" Property="Visibility" Value="Hidden" />
<Setter TargetName="SearchBoxButton" Property="Style" Value="{StaticResource CancelSearchButton}" />
</Trigger>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter TargetName="LabelText" Property="Visibility" Value="Hidden" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Upvotes: 1
Views: 8909
Reputation: 14611
you can use attached property
public static class TextBoxBehaviour
{
public static bool GetClearAll(TextBox target)
{
return (bool)target.GetValue(ClearAllAttachedProperty);
}
public static void SetClearAll(TextBox target, bool value)
{
target.SetValue(ClearAllAttachedProperty, value);
}
public static readonly DependencyProperty ClearAllAttachedProperty =
DependencyProperty.RegisterAttached("ClearAll", typeof(bool), typeof(TextBoxBehaviour), new UIPropertyMetadata(false, OnClearAllAttachedPropertyChanged));
static void OnClearAllAttachedPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
((TextBox)o).Clear();
}
}
usage
<ControlTemplate.Triggers>
<Trigger SourceName="SearchBoxButton"
Property="IsPressed"
Value="True">
<Setter Property="WpfStackOverflowSpielWiese:TextBoxBehaviour.ClearAll"
Value="True" />
</Trigger>
</ControlTemplate.Triggers>
Upvotes: 3
Reputation: 6733
use an event trigger on the style (not on the ControlTemplate)
http://msdn.microsoft.com/en-us/library/system.windows.eventtrigger.aspx
You can't put setters in an EventTrigger :(
However, you can use a storyboard :)
<Style>
<Style.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Text">
<DiscreteObjectKeyFrame Value="" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
Upvotes: 4