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

查看:22
本文介绍了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);
    }

我希望能够动态创建 ImageButton 并将它们填充到像 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>

作为ItemsControl 的用法,以URL 集合作为ItemsSource,您可以将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天全站免登陆