私がやっていることは、ページ内にある "testButton"ボタンをクリックしてウィンドウ内の枠内にあるイベントをトリガーするときですMainWindowの内部にあります。 EventTriggerからSourceName = "testButton"を削除すると、すべてのボタンで動作しますが、その特定のボタンでのみ機能します。WPF(XAML)のEventTrigger上の別のクラスのボタンにアクセスする
<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>
これはMMVMベースのアプリですか? – Ramankingdom
私は違いを知らない、私はXAML全体のことを初めて知った。 – Toody
あなたはもっと進歩しようとしているかもしれません。基本的なものと一緒に行くと、たくさんのことができます。イベントトリガーを読み取り、メインウィンドウの内部で直接起動しようとします。 – Ramankingdom