WPF:有没有一种方法将按钮点击事件传递给内容模板?

huangapple go评论52阅读模式
英文:

wpf: is there a way to passing button click event to content template?

问题

我想在内容模板中使用动画(示例是点击事件,实际上可能是其他事件甚至自定义事件),所以是否可能将事件传递给内容模板?

<Button Content="你好">
    <!--起作用-->
    <!--<Button.Triggers>
        <EventTrigger RoutedEvent="Button.Click">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To=".5" Duration="0:0:0.500"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Button.Triggers>-->

    <!--不起作用-->
    <Button.ContentTemplate>
        <DataTemplate>
            <Grid x:Name="Grid">
                <TextBlock Text="{Binding}"/>
            </Grid>
            <DataTemplate.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="Grid" Storyboard.TargetProperty="Opacity" From="1" To=".5" Duration="0:0:0.500"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </Button.ContentTemplate>
</Button>
英文:

I want to use animation in the content template(the example is click event,actually may be other event even custom event),so is it possible to passing the event to content template?

<Button Content="Hello">
    <!--work-->
    <!--<Button.Triggers>
        <EventTrigger RoutedEvent="Button.Click">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To=".5" Duration="0:0:0.500"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Button.Triggers>-->

    <!--not work-->
    <Button.ContentTemplate>
        <DataTemplate>
            <Grid x:Name="Grid">
                <TextBlock Text="{Binding}"/>
            </Grid>
            <DataTemplate.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="Grid" Storyboard.TargetProperty="Opacity" From="1" To=".5" Duration="0:0:0.500"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </Button.ContentTemplate>
</Button>

答案1

得分: 0

No,你可以向模板中的元素添加触发器,但不能向模板本身添加事件。 DataTemplate 不知道它是或可能被应用于 Button。 您只能向实际的 Button 元素添加 Click 事件触发器,该元素可能位于模板中。

英文:

>so is it possible to passing the event to content template?

No, you can add a trigger to an element in the template but not the template itself.

The DataTemplate is not aware that it is, or may be, applied to a Button.

You can only add a Click event trigger to an actual Button element, which may be located in the template.

答案2

得分: 0

你必须将触发器移到 ControlTemplateStyle 中。

如果你只想要动画化按钮的不透明度(而不是特定模板子元素的不透明度),只需将触发器移到 Button:Triggers 集合中。

请注意,如果您不向 DataTemplate 添加任何特殊元素,它是完全多余的。按钮将隐式添加一个 TextBlock 以托管文本内容。

除非您想要更改布局或覆盖默认布局行为,您应该在 Style 中实现触发器或直接将其添加到 Button.Triggers 集合中。

Button.Triggers 中的触发器

<Button Content="Hello!">
  <Button.Triggers>
    <EventTrigger RoutedEvent="Button.Click">
      <BeginStoryboard>
        <Storyboard>
          <DoubleAnimation Storyboard.TargetProperty="Opacity"
                           From="1"
                           To=".5"
                           Duration="0:0:0.500" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Button.Triggers>
</Button>

Style 中的触发器

<Button Content="Hello!">
  <Button.Style>
    <Style TargetType="Button">
      <Style.Triggers>
        <EventTrigger RoutedEvent="Button.Click">
          <BeginStoryboard>
            <Storyboard>
              <DoubleAnimation Storyboard.TargetProperty="Opacity"
                               From="1"
                               To=".5"
                               Duration="0:0:0.500" />
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </Style.Triggers>
    </Style>
  </Button.Style>
</Button>

ControlTemplate 中的触发器
请注意,此解决方案需要实现控件的默认行为,例如鼠标悬停效果。

<Button Content="Ah dutty and ah like it so!">
  <Button.Template>
    <ControlTemplate TargetType="Button">
      <Border Background="{TemplateBinding Background}"
              BorderBrush="{TemplateBinding BorderBrush}"
              BorderThickness="{TemplateBinding BorderThickness}">
        <ContentPresenter />
      </Border>
      <ControlTemplate.Triggers>
        <EventTrigger RoutedEvent="Button.Click">
          <BeginStoryboard>
            <Storyboard>
              <DoubleAnimation Storyboard.TargetProperty="Opacity"
                               From="1"
                               To=".5"
                               Duration="0:0:0.500" />
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>
  </Button.Template>
</Button>
英文:

You must move the trigger either to the ControlTemplate or to a Style.
If you are only interested in animating the button's opacity (and not the opacity of a particular template child, simply move the trigger to the Button:Triggers collection.

Note, if you don't add any special elements to the DataTemplate, it is completely redundant. The Button will implicitly add a TextBlock to host the text content

Unless you want to change the layout or override the default layout behavior you should implement the trigger in a Style or add it directly to the Button.Triggers collection:

Trigger in Button.Triggers

&lt;Button Content=&quot;Hello!&quot;&gt;
  &lt;Button.Triggers&gt;
    &lt;EventTrigger RoutedEvent=&quot;Button.Click&quot;&gt;
      &lt;BeginStoryboard&gt;
        &lt;Storyboard&gt;
          &lt;DoubleAnimation Storyboard.TargetProperty=&quot;Opacity&quot;
                           From=&quot;1&quot;
                           To=&quot;.5&quot;
                           Duration=&quot;0:0:0.500&quot; /&gt;
        &lt;/Storyboard&gt;
      &lt;/BeginStoryboard&gt;
    &lt;/EventTrigger&gt;
  &lt;/Button.Triggers&gt;
&lt;/Button&gt;

Trigger in Style

&lt;Button Content=&quot;Hello!&quot;&gt;
  &lt;Button.Style&gt;
    &lt;Style TargetType=&quot;Button&quot;&gt;
      &lt;Style.Triggers&gt;
        &lt;EventTrigger RoutedEvent=&quot;Button.Click&quot;&gt;
          &lt;BeginStoryboard&gt;
            &lt;Storyboard&gt;
              &lt;DoubleAnimation Storyboard.TargetProperty=&quot;Opacity&quot;
                               From=&quot;1&quot;
                               To=&quot;.5&quot;
                               Duration=&quot;0:0:0.500&quot; /&gt;
            &lt;/Storyboard&gt;
          &lt;/BeginStoryboard&gt;
        &lt;/EventTrigger&gt;
      &lt;/Style.Triggers&gt;
    &lt;/Style&gt;
  &lt;/Button.Style&gt;
&lt;/Button&gt;

Trigger in ControlTemplate
Note, this solution requires to implement the default behavior of the control for example mouse over effects.

&lt;Button Content=&quot;Ah dutty and ah like it so!&quot;&gt;
  &lt;Button.Template&gt;
    &lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
      &lt;Border Background=&quot;{TemplateBinding Background}&quot;
              BorderBrush=&quot;{TemplateBinding BorderBrush}&quot;
              BorderThickness=&quot;{TemplateBinding BorderThickness}&quot;&gt;
        &lt;ContentPresenter /&gt;
      &lt;/Border&gt;
      &lt;ControlTemplate.Triggers&gt;
        &lt;EventTrigger RoutedEvent=&quot;Button.Click&quot;&gt;
          &lt;BeginStoryboard&gt;
            &lt;Storyboard&gt;
              &lt;DoubleAnimation Storyboard.TargetProperty=&quot;Opacity&quot;
                               From=&quot;1&quot;
                               To=&quot;.5&quot;
                               Duration=&quot;0:0:0.500&quot; /&gt;
            &lt;/Storyboard&gt;
          &lt;/BeginStoryboard&gt;
        &lt;/EventTrigger&gt;
      &lt;/ControlTemplate.Triggers&gt;
    &lt;/ControlTemplate&gt;
  &lt;/Button.Template&gt;
&lt;/Button&gt;

huangapple
  • 本文由 发表于 2023年6月9日 10:50:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定