对于按钮可重复使用的自定义内容 [英] Reusable Custom Content for Buttons

查看:142
本文介绍了对于按钮可重复使用的自定义内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用户界面使用户使用定制按钮取值:他们同时包含图片标签

My user interface makes use of customized Buttons: They contain both an Image and a Label.

我手工定制一个按钮,由持有图片,其内容设置为一个网格中的标签。然而,因为我需要有几个这样的按钮 S,用不同的图像和标签,我想提取这种模式弄成重用。基本上,我只是需要一个可重用的对象,具有2个属性(图片文本),我可以设置为若干按钮思想内容

I customized a single Button manually, by setting its content to a grid holding an Image and a Label. However, since I need to have several such Buttons, with different images and labels, I'd like to "extract" this pattern into something reusable. Basically, I just need a reusable object, with 2 properties (Image and Text) that I can set as the Content of several Buttons.

我看着ContentTemplates,但我并不需要定制的按钮控制本身,不仅仅是其内容的外观。

I looked at ContentTemplates, but I do not need to customize the appearance of the Button control itself, just its content.

什么是使用这个最合适的技术?

What is the most appropriate technique to use for this?

推荐答案

做最简单的事情就是创造这样的用户控件:

The easiest thing to do is create a UserControl for this:


  1. 创建一个用户控件

  2. 添加ImageSource的依赖属性的用户控件类类型的BitmapSource为图片

  3. 一个文本依赖属性添加到用户控件类类型的字符串

  4. 为您的用户控件XAML,把下面的XAML(简化简洁,你可以改变,以适应您的布局需要)

XAML的用户控件:

<UserControl DataContext="{Binding RelativeSource={RelativeSource Self}}" ...>
    <Button>
        <StackPanel Orientation="Horizontal">
           <Image Source="{Binding ImageSource}" />
           <TextBlock Text="{Binding Text}" />
        </StackPanel>
    </Button>
</UserControl>

现在,你可以重用的用户控件,无论你想,像这样:

Now you can reuse your user control wherever you want like so:

<myNS:MyUserControl ImageSource="MyImages/Foo.png" Text="Click here!" />

这篇关于对于按钮可重复使用的自定义内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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