如何给动态创建的按钮控件设置背景图片?

发布于 2024-10-16 12:36:09 字数 369 浏览 3 评论 0原文

我正在开发 Windows Phone 7 应用程序。我是 Windows Phone 7 应用程序的新手。我的应用程序中有列表框 &我动态创建按钮控件并将它们添加到列表框控件。现在我想为每个动态创建的按钮控件设置背景图像。我使用以下代码动态设置按钮控件的背景颜色

Button AlphabetButton = new Button();
                AlphabetButton.Background = new SolidColorBrush(Colors.Red);

通过这种方式,我想为动态创建的按钮控件设置背景图像,而不是背景颜色。如何做到这一点?您能否提供任何代码或链接来解决上述问题。如果我做错了什么,请指导我。

I am developing window phone 7 application. I am new to the window phone 7 application. I have Listbox in my application & I am dynamically creating the button control and adding them to the listbox control. Now I want to set the background image for each dynamically created button control. I am using the following code to dynamically set the background color of button control

Button AlphabetButton = new Button();
                AlphabetButton.Background = new SolidColorBrush(Colors.Red);

In this way instead of background color I want to set the background image for my dynamically created button control. How to do this ? Can you please provide me any code or link through which I can resolve the above issue. If I am doing anything wrong then please guide me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

财迷小姐 2024-10-23 12:36:09

试试这个:

    ImageBrush brush = new ImageBrush();
    brush.ImageSource = new BitmapImage(new Uri(@"Images/myImage.png", UriKind.Relative)); 
    AlphabetButton.Background = brush;

另一种选择是使用按钮内容,例如:

   Uri uri = new Uri("/images/someImage.png", UriKind.Relative);  
   BitmapImage imgSource = new BitmapImage(uri);
   Image image = new Image();
   image.Source = imgSource;
   AlphabetButton.Content = image;

小心图像构建操作。

Try this:

    ImageBrush brush = new ImageBrush();
    brush.ImageSource = new BitmapImage(new Uri(@"Images/myImage.png", UriKind.Relative)); 
    AlphabetButton.Background = brush;

Another option is to use the Button Content like for example:

   Uri uri = new Uri("/images/someImage.png", UriKind.Relative);  
   BitmapImage imgSource = new BitmapImage(uri);
   Image image = new Image();
   image.Source = imgSource;
   AlphabetButton.Content = image;

Be careful with the image Build Action.

-小熊_ 2024-10-23 12:36:09

更好的方法是使用数据绑定...例如,您有一个要在列表框中显示的 Person 对象列表,在代码隐藏中将该列表设置为 ItemsSource:

public class Person()
{
  public string Name {get; set;}
}

var persons = new List<Person>();
// ... add some person objects here ...
listBox.ItemsSource = persons

然后在 XAML 中您可以提供一个 DataTemplate将每个人呈现为一个按钮:

<ListBox x:Name="listBox">
  <Listox.ItemTemplate>
     <DataTemplate>
        <Button Content={Binding Path=Name}/>
     </DataTemplate>
  </Listox.ItemTemplate>
</ListBox>

这将呈现一个显示每个人姓名的按钮列表。

要为每个按钮指定图像,请扩展按钮的内容以包含图像:

<ListBox x:Name="listBox">
  <Listox.ItemTemplate>
     <DataTemplate>
        <Button>
          <StackPanel Orientation="Horizontal">
             <TextBlock Text="{Binding Path=Name}"/>
             <ImageText Source="{Binding Path=Image}"/>
          </StackPanel>
        </Button>
     </DataTemplate>
  </Listox.ItemTemplate>
</ListBox>

当然,您必须向 Person 对象添加 ImageSource 属性:

public class Person()
{
  public string Name {get; set;}
  public ImageSource Image {get; set;}
}

另一种选择是使用值转换器将 Person 的某些属性转换为image:

silverlight 中的动态图像源绑定

如果您不想使用绑定(我个人认为这是最好的方法)您可以创建一个具有 ImageSource 属性的 Button 子类,如下所示:

使用控件模板创建图像+文本按钮?

A much better approach would be to use databinding ... say for example you have a list of Person objects that you want to display in your listbox, in codebehind set the list as the ItemsSource:

public class Person()
{
  public string Name {get; set;}
}

var persons = new List<Person>();
// ... add some person objects here ...
listBox.ItemsSource = persons

In you XAML you can then supply a DataTemplate that renders each person as a Button:

<ListBox x:Name="listBox">
  <Listox.ItemTemplate>
     <DataTemplate>
        <Button Content={Binding Path=Name}/>
     </DataTemplate>
  </Listox.ItemTemplate>
</ListBox>

This will render a list of Buttons which display the name of each person.

To specify an image for each button extend the content of your button to include an image:

<ListBox x:Name="listBox">
  <Listox.ItemTemplate>
     <DataTemplate>
        <Button>
          <StackPanel Orientation="Horizontal">
             <TextBlock Text="{Binding Path=Name}"/>
             <ImageText Source="{Binding Path=Image}"/>
          </StackPanel>
        </Button>
     </DataTemplate>
  </Listox.ItemTemplate>
</ListBox>

You will of course have to add an ImageSource property to your Person object:

public class Person()
{
  public string Name {get; set;}
  public ImageSource Image {get; set;}
}

Another alternative is to use a value converter to convert some property of your Person into an image:

Dynamic image source binding in silverlight

If you do not want to use binding (which I personally think is the best approach) you could create a Button subclass that has an ImageSource property as per the following:

Creating an image+text button with a control template?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文