[英]Access a button from another class on EventTrigger in WPF (XAML)
我想要做的是單擊頁面內的按鈕“ testButton”以觸發事件,該事件位於MainWindow內Window.Resource內的Border內。 如果我從EventTrigger中刪除SourceName =“ testButton”,則它適用於每個按鈕,我想要的是僅適用於該特定按鈕。
<Window x:Class="test.mainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:test"
mc:Ignorable="d">
<Window.Resources>
<Style TargetType="{x:Type local:mainWindow}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Border Padding="5">
<Border.Background>
<SolidColorBrush x:Name="bgBrush" Color="Transparent" />
</Border.Background>
<Border.Triggers>
<EventTrigger RoutedEvent="Button.Click" SourceName="testButton">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="bgBrush"
Storyboard.TargetProperty = "Color"
From="Transparent"
To = "White"
Duration="0:0:01.00"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Border.Triggers>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
</Window>
這是按鈕所在的頁面:
<Page x:Class="test.testPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:test"
mc:Ignorable="d" Width="300" Height="200">
<Grid>
<Button x:Name="testButton" Height="20" Width="50" Content="Test"/>
</Grid>
</Page>
我共享的示例代碼將在單擊Page中的按鈕時將MainWindow背景色更改為紅色 。 它可以幫助您了解路由事件。有不同的方法。 我創建了一個自定義事件來實現此目的。 這是您的代碼https://drive.google.com/file/d/0B4bUNjAQRJpWSEpLV1JJeXQ4N1k/view?usp=sharing
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.