silverlight自定义控件 10

要实现的功能如下:定义一个按钮,按钮中有图片。完成后图片可以根据属性自由设置,不能说每用一次控件都得在下Content属性中添加Image控件。求demo或解决方法.... 要实现的功能如下:定义一个按钮,按钮中有图片。完成后图片可以根据属性自由设置,不能说每用一次控件都得在下Content属性中添加Image控件。求demo或解决方法. 展开
 我来答
真的还可以吧
2012-03-16 · TA获得超过112个赞
知道答主
回答量:197
采纳率:0%
帮助的人:136万
展开全部

这个不难,重定义控件就ok

代码给你参考:

类1:

[TemplatePart(Name = BorderName, Type = typeof(Border))]

    public class ImageItem : ButtonBase

    {

        private const string BorderName = "Part_Border";

        public ImageItem()

        {

            this.DefaultStyleKey = typeof(ImageItem);

            this.MouseEnter += new MouseEventHandler(ImageItem_MouseEnter);

            this.MouseLeave += new MouseEventHandler(ImageItem_MouseLeave);

        }

        void ImageItem_MouseLeave(object sender, MouseEventArgs e)

        {

            var border = this.GetTemplateChild(BorderName) as Border;

            border.BorderBrush = new SolidColorBrush(Colors.Transparent);

        }

        void ImageItem_MouseEnter(object sender, MouseEventArgs e)

        {

            var border = this.GetTemplateChild(BorderName) as Border;

            border.BorderBrush = new SolidColorBrush(Colors.Orange);

        }

        # region ImageSource

        public static readonly DependencyProperty ImageSourceProperty =

            DependencyProperty.Register("ImageSource", typeof(ImageSource),

           typeof(ImageItem), new PropertyMetadata(null));

        public ImageSource ImageSource

        {

            get { return (ImageSource)GetValue(ImageSourceProperty); }

            set { SetValue(ImageSourceProperty, value); }

        }

        # endregion

        # region Caption

        public static readonly DependencyProperty CaptionProperty =

           DependencyProperty.Register("Caption", typeof(string),

           typeof(ImageItem), new PropertyMetadata(string.Empty));

        public string Caption

        {

            get { return (string)GetValue(CaptionProperty); }

            set { SetValue(CaptionProperty, value); }

        }

        #endregion

    }

类2:

[TemplatePart(Name = BorderName, Type = typeof(Border))]

    public class ImageButton : ImageItem

    {

        private const string BorderName = "Part_Border";

        private Brush borderBrush;

        public ImageButton()

        {

            this.DefaultStyleKey = typeof(ImageButton);

            this.MouseEnter += ImageButton_MouseEnter;

            this.MouseLeave += ImageButton_MouseLeave;

        }

        void ImageButton_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)

        {

            var border = this.GetTemplateChild(BorderName) as Border;

            borderBrush = border.BorderBrush;

            border.BorderBrush = new SolidColorBrush(Colors.Transparent);

            border.Background = new SolidColorBrush(Colors.Transparent);

        }

        void ImageButton_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)

        {

            var border = this.GetTemplateChild(BorderName) as Border;

            border.BorderBrush = borderBrush;

            border.Background = new SolidColorBrush(Color.FromArgb(100, 142, 217, 205));

        }:

    }

使用时加入头文件:xmlns:my="clr-namespace:***类所在的域名空间*****"

//样式 1

            <Style TargetType="my:ImageButton">

                <Setter Property="Template">

                    <Setter.Value>

                        <ControlTemplate TargetType="my:ImageButton">

                            <Border Background="Transparent" CornerRadius="4"

                              BorderBrush="{TemplateBinding BorderBrush}"

                              BorderThickness="{TemplateBinding BorderThickness}" x:Name="Part_Border">

                                <StackPanel Orientation="Horizontal"  Cursor="Hand" Margin="2">

                                    <Image Source="{TemplateBinding ImageSource}" Stretch="None" />

                                    <TextBlock x:Name="Part_Caption" Text="{TemplateBinding Caption}" Margin="5,0,0,0" FontSize="12" />

                                </StackPanel>

                            </Border>

                        </ControlTemplate>

                    </Setter.Value>

                </Setter>

            </Style>

<my:ImageButton  Caption="返回" ImageSource="/BANK-S;component/Images/image06.png" Width="70" Command="{Binding Return}"/>

/*

Caption 相对与“按钮一”

 ImageSource 图片路径

Command 点击事件

*/

效果跟你提供的图片一样。

//样式 2

        <Style TargetType="my:ImageItem">

            <Setter Property="Control.Template">

                <Setter.Value>

                    <ControlTemplate TargetType="my:ImageItem">

                        <Border

                        x:Name="Part_Border"

CornerRadius="2.5"

BorderBrush="Transparent"

BorderThickness="2">

                            <StackPanel Orientation="Vertical" Cursor="Hand" Margin="5">

                                <Image Width="48" Height="48"  Source="{TemplateBinding ImageSource}" />

                                <TextBlock HorizontalAlignment="Center" Text="{TemplateBinding Caption}" FontSize="12"  />

                            </StackPanel>

                        </Border>

                    </ControlTemplate>

                </Setter.Value>

            </Setter>

        </Style>

  <my:ImageItem Caption="按钮一" Width="84" Height="80" Margin="2,10,1,10" ImageSource="/BANK-S;component/Images/pic.png" Command="{Binding Data}" >

/*

Caption 文字“按钮一”

 ImageSource 图片路径

Command 点击事件

*/

效果截图。

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式