英文:
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
你必须将触发器移到 ControlTemplate
或 Style
中。
如果你只想要动画化按钮的不透明度(而不是特定模板子元素的不透明度),只需将触发器移到 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
<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>
Trigger in 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>
Trigger in ControlTemplate
Note, this solution requires to implement the default behavior of the control for example mouse over effects.
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论