cka*_*ass 6 silverlight user-controls silverlight-2-rc0
我刚刚开始使用Silverlight(2 RC0)并且似乎无法使用以下内容.我想创建一个简单的图像按钮用户控件.
我的用户控件的xaml如下:
<Button>
<Button.Template>
<ControlTemplate>
<Image Source="{TemplateBinding ImageSource}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" />
</ControlTemplate>
</Button.Template>
</Button>
Run Code Online (Sandbox Code Playgroud)
背后的代码如下:
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);
}
Run Code Online (Sandbox Code Playgroud)
我希望能够动态创建ImageButtons并将它们填充到像WrapPanel这样的容器中:假设我们已经有一个名为"image"的图像:
ImageButtonUserControl imageButton = new ImageButtonUserControl();
imageButton.Source = image;
this.thumbnailStackPanel.Children.Add(imageButton);
Run Code Online (Sandbox Code Playgroud)
我需要做什么才能显示图像?我假设我需要对DataContext做一些事情,但我不太清楚什么或在哪里.
谢谢你的帮助
Job*_*Joy 10
您可以通过模板化普通Button轻松获得ImageButton,这样您根本不需要UserControl.假设Button.Content将是ImageSource.Button的ControlTemplate将是:
<ControlTemplate x:Key="btn_template">
<Image Source="{TemplateBinding Content}" />
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
作为ItemsControl使用URL集合作为其ItemsSource,您可以添加WrapPanel作为ItemPanel.如果您没有指定,则默认为StackPanel.
<DataTemplate x:Key="dataTemplate">
<Button Template="{StaticResource btn_template}" Content="{Binding}"/>
</DataTemplate>
<ItemsControl ItemsSource="{Binding UrlCollection}" ItemsTemplate="{StaticResource dataTemplate}"/>
Run Code Online (Sandbox Code Playgroud)