WPF使用代码加载自定义控件

时间:2013-06-23 16:13:14

标签: wpf custom-controls

我想创建一个“按钮”,当用户点击时,该按钮会弹出鼠标位置。我在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文件。

<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. -->

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分: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>

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