Silverlight ImageButton用户控件 [英] Silverlight ImageButton UserControl

查看:87
本文介绍了Silverlight ImageButton用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Silverlight(2 RC0),但似乎无法使以下内容正常工作.我想创建一个简单的图像按钮用户控件.

I am just starting out with Silverlight (2 RC0) and can’t seem to get the following to work. I want to create a simple image button user control.

我用于用户控件的xaml如下:

My xaml for the user control is as follows:

 <Button>
        <Button.Template>   
            <ControlTemplate>
                <Image Source="{TemplateBinding ImageSource}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" />
            </ControlTemplate>
        </Button.Template>
    </Button>

后面的代码如下:

public partial class ImageButtonUserControl : UserControl
    {
        public ImageButtonUserControl()
        {
            InitializeComponent();
        }

        public Image Source
        {
            get { return base.GetValue(SourceProperty) as Image; }
            set { base.SetValue(SourceProperty, value); }
        }
        public static readonly DependencyProperty SourceProperty = 
            DependencyProperty.Register("SourceProperty", typeof(Image), typeof(ImageButtonUserControl),null);
    }

我希望能够动态创建ImageButtons并将其填充到类似WrapPanel的容器中: 假设我们已经有一个名为"image"的图像:

I want to be able to dynamically create the ImageButtons and stuff them in a container like a WrapPanel: Assume we have an image named "image" already:

ImageButtonUserControl imageButton = new ImageButtonUserControl();
imageButton.Source = image;
this.thumbnailStackPanel.Children.Add(imageButton);

我需要怎么做才能显示图像?我以为我需要对DataContext做些事情,但是我不确定是什么地方.

What do I need to do to get the image to display? I'm assuming I need to do something with DataContext, but I'm not quite sure what or where.

感谢您的帮助

推荐答案

只需模板化一个普通Button,就可以轻松获得ImageButton,因此根本不需要UserControl.假设Button.Content将是ImageSource.按钮的ControlTemplate将为:

You can get an ImageButton easily just by templating an ordinary Button so you dont require a UserControl at all. Assuming that Button.Content will be the ImageSource. The ControlTemplate of the Button will be:

 <ControlTemplate x:Key="btn_template">         
   <Image Source="{TemplateBinding Content}" />   
 </ControlTemplate>

并用作URL集合作为ItemsSource的ItemsControl,可以将WrapPanel添加为ItemPanel.如果不指定,默认值为StackPanel.

And the usage as an ItemsControl with URL collection as its ItemsSource, You can add WrapPanel as the ItemPanel. Default will be StackPanel if you don't specify one.

<DataTemplate x:Key="dataTemplate">
  <Button Template="{StaticResource btn_template}" Content="{Binding}"/>
</DataTemplate>    


 <ItemsControl ItemsSource="{Binding UrlCollection}" ItemsTemplate="{StaticResource dataTemplate}"/>

这篇关于Silverlight ImageButton用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆