在WP8中创建自定义按钮控件

时间:2014-09-07 13:39:19

标签: c# xaml windows-phone-8

...


好的,谢谢Scott Nimrod,我找到了一种方法:创建一个新项目,添加一个新的用户控件,构建到dll并添加对我的app项目的引用

这是我的代码,如果有人需要的话:):

在我的应用程序的xaml页面中(点击事件有效但2图像" sourceNormal"" sourcePressed"不可见)

<local:ButtonImage Width="40" Height="40"
                         Click="ButtonImage_Click"
                         SourceNormal="/download_home.png"                             
                         SourcePressed="/download_nowplaying.png"/>

UserControl XAML

<UserControl x:Class="MyCustomControl.ButtonImage"
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"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480"

DataContext="{Binding RelativeSource={RelativeSource Self}}"         
xmlns:ntd="clr-namespace:MyCustomControl">

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Button x:Name="Button_Image" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <Grid Background="Transparent">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ImageNormal">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility>Visible</Visibility>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ImagePressed">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility>Collapsed</Visibility>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="MouseOver"/>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ImageNormal">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility>Collapsed</Visibility>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ImagePressed">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility>Visible</Visibility>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Image x:Name="ImageNormal" VerticalAlignment="Center" HorizontalAlignment="Center" Source="{Binding SourceNormal}" Stretch="Uniform"/>
                    <Image x:Name="ImagePressed" VerticalAlignment="Center" HorizontalAlignment="Center" Source="{Binding SourcePressed}" Stretch="Uniform"/>                        
                </Grid> 
            </ControlTemplate>
        </Button.Template>
    </Button>

</Grid>

后面的UserControl代码:

namespace MyCustomControl
{
public partial class ButtonImage : UserControl
{
    public ButtonImage()
    {
        InitializeComponent();            
        Button_Image.Click += ButtonImage_Click;
    }

    public ImageSource SourceNormal
    {
        get { return (ImageSource)GetValue(SourceNormalProperty);}
        set { SetValue(SourceNormalProperty, value); }
    }        
    public ImageSource SourcePressed
    {
        get { return (ImageSource)GetValue(SourcePressedProperty); }
        set { SetValue(SourcePressedProperty, value); }
    }

    public event EventHandler Click;
    void ButtonImage_Click(object sender, RoutedEventArgs e)
    {
        var eventHandler = this.Click;
        if (eventHandler != null)
        {
            eventHandler(this, e);
        }
    }



    /////////////////////// SourceNormal /////////////////////////////////////////////////////

    //----------------------------------------------------------------------------------------
    public static readonly DependencyProperty SourceNormalProperty = DependencyProperty.RegisterAttached(
        "SourceNormal",
        typeof(ImageSource),
        typeof(ButtonImage),
        new PropertyMetadata(null)
    );

    public static ImageSource GetSourceNormal(UIElement element)
    {
        if (element == null)
        {
            new ArgumentNullException("element");
        }
        return (ImageSource)element.GetValue(SourceNormalProperty);
    }

    public static void SetSourceNormal(UIElement element, ImageSource value)
    {
        if (element == null)
        {
            new ArgumentNullException("element");
        }

        element.SetValue(SourceNormalProperty, value);
    }
    //----------------------------------------------------------------------------------------

    /////////////////////// SourcePressed ////////////////////////////////////////////////////

    //----------------------------------------------------------------------------------------
    public static readonly DependencyProperty SourcePressedProperty = DependencyProperty.RegisterAttached(
        "SourcePressed",
        typeof(ImageSource),
        typeof(ButtonImage),
        new PropertyMetadata(null)
    );

    public static ImageSource GetSourcePressed(UIElement element)
    {
        if (element == null)
        {
            new ArgumentNullException("element");
        }
        return (ImageSource)element.GetValue(SourcePressedProperty);
    }

    public static void SetSourcePressed(UIElement element, ImageSource value)
    {
        if (element == null)
        {
            new ArgumentNullException("element");
        }

        element.SetValue(SourcePressedProperty, value);
    }
}

}

1 个答案:

答案 0 :(得分:1)

答案是肯定的。

Telerik使用他们的RadControls做到这一点。

只需创建一个单独的项目并添加WPF为UI所依赖的主要引用。 这也是实现Prism模块时的一种模式。

在其他项目中,只需添加对定义样式的dll的引用。

相关问题