从WPF文本框中的样式中提升按钮单击事件

时间:2012-03-06 07:05:01

标签: wpf xaml

我有一个WPF文本框的样式,看起来像这样。模板上有一个按钮,其按钮点击事件我需要将文本设置为空。有人能说出我错过了什么。或者如何调用自定义SearchTextBox中的方法说(OnButtonClikced)自定义方法

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

2 个答案:

答案 0 :(得分:4)

在样式上使用事件触发器(不在ControlTemplate上)

http://msdn.microsoft.com/en-us/library/system.windows.eventtrigger.aspx

你不能把setter放在EventTrigger中:(

但是,您可以使用故事板:)

<Style>
  <Style.Triggers>
    <EventTrigger RoutedEvent="Button.Click">
      <BeginStoryboard>
        <Storyboard>
          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Text">
            <DiscreteObjectKeyFrame Value="" />
          </ObjectAnimationUsingKeyFrames>
        </Storyboard>
      </BeginStoryboard>        
    </EventTrigger>
  </Style.Triggers>
</Style>

答案 1 :(得分:2)

你可以使用附加财产

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();
    }
}

使用

<ControlTemplate.Triggers>

  <Trigger SourceName="SearchBoxButton"
           Property="IsPressed"
           Value="True">
    <Setter Property="WpfStackOverflowSpielWiese:TextBoxBehaviour.ClearAll"
            Value="True" />
  </Trigger>

</ControlTemplate.Triggers>