0

我想创建一个在用户单击时在鼠标位置弹出的“按钮”。我在 Blend 中制作了带有加载事件动画的按钮,并将其放入 ResourceDictionary。

当我使用 xaml 在画布上加载按钮时它可以工作,但是当我尝试将加载代码放在 MouseDown 事件处理程序中时它不起作用 - 按钮不会弹出。

有人可以帮我吗?谢谢!

这是我用来加载按钮的代码(不起作用)。

        private void canvas_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        Button popupButton = new Button();
        popupButton.Template = (ControlTemplate)Resources["ButtonControlTemplate1"];
        canvas.Children.Add(popupButton);
        Canvas.SetLeft(popupButton, Mouse.GetPosition(canvas).X);
        Canvas.SetTop(popupButton, Mouse.GetPosition(canvas).Y);
    }

这是有效的 MainWindow.xaml。

<Window x:Class="CustomPopupMenu01.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="428" Width="739">
<Grid x:Name="grid" >
    <Canvas Name="canvas" HorizontalAlignment="Left" Height="373" VerticalAlignment="Top" Width="682"  Background="White" MouseDown="canvas_MouseDown">
        <Button Content="Button" Height="81.333" Canvas.Left="187.333" Canvas.Top="99.667" Width="126" Template="{DynamicResource ButtonControlTemplate1}"/>
    </Canvas>
</Grid>

这是资源字典文件。

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ControlTemplate x:Key="ButtonControlTemplate1" TargetType="{x:Type Button}">
    <ControlTemplate.Resources>
        <Storyboard x:Key="OnLoaded1">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="grid">
                <EasingDoubleKeyFrame KeyTime="0" Value="0">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <QuarticEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <QuarticEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="grid">
                <EasingDoubleKeyFrame KeyTime="0" Value="0">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <QuarticEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <QuarticEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="grid">
                <EasingDoubleKeyFrame KeyTime="0" Value="-25">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <QuarticEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <QuarticEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="grid">
                <EasingDoubleKeyFrame KeyTime="0" Value="-25">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <QuarticEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <QuarticEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Key="Storyboard1">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="grid">
                <EasingDoubleKeyFrame KeyTime="0" Value="-25">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <QuinticEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <QuinticEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="grid">
                <EasingDoubleKeyFrame KeyTime="0" Value="-25">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <QuinticEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <QuinticEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="grid">
                <EasingDoubleKeyFrame KeyTime="0" Value="0">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <QuinticEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="1">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <QuinticEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="grid">
                <EasingDoubleKeyFrame KeyTime="0" Value="0">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <QuinticEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="1">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <QuinticEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>

    </ControlTemplate.Resources>
    <Grid x:Name="grid">
            <Grid.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                </TransformGroup>
            </Grid.RenderTransform>

        <Ellipse HorizontalAlignment="Left" Height="50" Margin="0" Stroke="Black" StrokeThickness="2" VerticalAlignment="Top" Width="50">
            <Ellipse.Fill>
                <LinearGradientBrush
EndPoint="0.854,0.854" StartPoint="0.146,0.146">
                    <GradientStop
Color="#FFBFCDDB" Offset="0"/>
                    <GradientStop
Color="White" Offset="0.603"/>
                </LinearGradientBrush>
            </Ellipse.Fill>
        </Ellipse>
        <Path Data="M7.5,19.333333 L40.138168,19.333333" HorizontalAlignment="Left" Height="2" Margin="7.833,19.333,0,0" Stretch="Fill" Stroke="#FF9C9C9C" StrokeThickness="1" VerticalAlignment="Top" Width="34.638" Fill="{x:Null}"/>
        <Path Data="M7.5,19.333333 L40.138168,19.333333" HorizontalAlignment="Left" Height="2" Margin="7.833,29.166,0,0" Stretch="Fill" Stroke="#FF9C9C9C" StrokeThickness="1" VerticalAlignment="Top" Width="34.638" Fill="{x:Null}"/>
        <Path Data="M24.666667,19.333 L21.333167,22.6665" Fill="White" HorizontalAlignment="Left" Height="6.834" Margin="21.833,16.5,0,0" Stretch="Fill" Stroke="Black" VerticalAlignment="Top" Width="6.834" StrokeThickness="2"/>
        <Path Data="M24.666667,19.333 L21.333167,22.6665" Fill="White" HorizontalAlignment="Left" Height="6.834" Margin="21.833,26.333,0,0" Stretch="Fill" Stroke="Black" VerticalAlignment="Top" Width="6.834" StrokeThickness="2"/>
    </Grid>
    <ControlTemplate.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Storyboard="{StaticResource OnLoaded1}"/>
        </EventTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>
<!-- Resource dictionary entries should be defined here. -->

.

4

2 回答 2

0

这可能取决于管理 wpf 树的顺序。

可以在您的按钮上调用 ApplyTemplate () 吗?

http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.applytemplate.aspx

于 2013-06-23T17:44:17.890 回答
0

假设您将资源添加到单独的 ResourceDictionary 中,类似于:

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ResourceDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

更改您的代码以在应用程序级别查找资源:

popupButton.Template = (ControlTemplate)App.Current.FindResource("ButtonControlTemplate1");

Resources后面代码中定义的仅适用于本地资源。所以这些将是在Window自身上定义的资源:

<Window>
    <Window.Resources>
        <!-- Resources has access here -->
    </Window.Resources>
</Window>

当您在应用程序级别定义资源并将它们包含为 a 的一部分时ResourceDictionary,您必须在应用程序中显式搜索以找到它们,因为默认情况下它们在本地不可用。

于 2013-06-23T17:56:21.717 回答