2017-08-04 71 views
0

我想要做的是当我点击一个页面内的按钮“testButton”来触发一个窗口内的边框内的事件。MainWindow内部的资源。如果我从EventTrigger中删除SourceName =“testButton”,它将与每个按钮一起工作,我想要的只是使用该特定按钮。访问WPF中的EventTrigger上的另一个类的按钮(XAML)

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

这是一个基于MMVM的应用程序还是正常? – Ramankingdom

+0

我不知道区别,我是新来的整个XAML的东西。 – Toody

+0

然后你可能会尝试更多的进步。随着基础知识将帮助你很多。阅读事件触发器并尝试在主窗口内直接启动。 – Ramankingdom

回答

0

我分享示例代码,这将你的主窗口背景颜色变为红色上单击页面按钮。它会帮助你理解路由事件。有不同的方法。我创建了一个自定义事件来实现这一点。这里是你的代码https://drive.google.com/file/d/0B4bUNjAQRJpWSEpLV1JJeXQ4N1k/view?usp=sharing

+0

我有另一个问题,我想要做的是设置这种风格来改变背景颜色的边界,而不是孔mainWindow,我试过这样的: 1.改变了这一点 - >