从另一个类或窗口访问WPF窗口中的控件

时间:2014-10-06 22:27:31

标签: wpf xaml

我有一个名为x:Name="wSplash"的窗口,其中包含一个按钮x:Name="MyButton" 我在一个单独的页面中有一个ResourceDictionary,其中包含一些Storyboards

我想使用 XAML

ResourceDictionary中的动画应用到wSplash窗口中的按钮

我尝试了Storyboard.TargetName="wSplash.MyButton"Storyboard.TargetName="(wSplash.MyButton)",但我得到了一个例外:无法解析TargetName。

有没有办法用 XAML

来做到这一点

wSplash.xaml:

<Window x:Class="wSplash" x:Name="wSplash"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="The Project" WindowStyle="None" ResizeMode="NoResize" AllowsTransparency="True" 
    WindowStartupLocation="CenterScreen" Height="350" Width="525" Background="Transparent">
    <Border BorderBrush="#809FD3" Background="#222" BorderThickness="1" CornerRadius="5">
        <Canvas>
            <Button x:Name="MyButton" Content="Hello" />
            </Canvas>
        </Border>
    </Window>

Animations.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Storyboard x:Key="AnSplash">
        <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)"
                         From="-15" To="13" Duration="0:0:0:01" />
        <DoubleAnimation Storyboard.TargetProperty="(Canvas.Top)"
                         From="-15" To="101" Duration="0:0:0:03" />
        <DoubleAnimation Storyboard.TargetProperty="(Canvas.Top)"
                         From="357" To="168" Duration="0:0:0:05" />
        <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)"
                         From="540" To="166" Duration="0:0:0:07" />
    </Storyboard>

</ResourceDictionary>

wSplash.xaml.vb:

 Public Sub MyEvent()
        Dim x As Storyboard = FindResource("AnSplash")
        x.Begin()
    End Sub

当然,我将 Application.xaml 中的 Animations.xaml 包含为MergedDictionaries

0 个答案:

没有答案