我想创建一个在用户单击时在鼠标位置弹出的“按钮”。我在 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. -->
.