Put pictures in buttons

I am new to this forum and programming. I have advised the developer community to clarify my doubts. I apologize because my English is not good, so I have to use the google translator and possibly the text is not perfect, and the lack of information on my part. I've been searching my question before posting my message, but I have not found anything.

I want to put images in buttons, so far I have managed to make a query in which I show in a listbox addresses the images you want to put the buttons.

        var obtenerImagen = (from n in basedeDatos.tablaProductos               
                             select n.imagenProducto);                          

        listBox1.ItemsSource = obtenerImagen;

Now my idea is to put the images on buttons from the query.

        ImageSourceConverter conversor = new ImageSourceConverter();

        image1.Source = (ImageSource)conversor.ConvertFromString(obtenerImagen);  /**/

        image1 = obtenerImagen.FirstOrDefault();  /**/

this gives me the following error lines: (/**/)

  • Argument 1: cannot convert from 'System.Linq.IQueryable' to 'string'
  • Cannot implicitly convert type 'string' to 'System.Windows.Controls.Image'
  • The best overloaded method match for 'System.ComponentModel.TypeConverter.ConvertFromString(string)' has some invalid arguments

What am I doing wrong?

Answers


Your object obtenerImagen is an Object of type IQueryable.

Maybe you should use:

var obtenerImagen = (from n in basedeDatos.tablaProductos               
                             select n.imagenProducto).ToList(); 

Then you have a list of strings. Now you can get the first-element for example by calling:

string imageSource = obtenerImagen.FirstOrDefault();

You need to define an item template for your listbox. I will show you an example:

<ListBox x:Name="listBox1>
    <ListBox.ItemTemplate>
      <DataTemplate>
        <Image Source="{Binding}" />
      </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Then you can use your code to set the listbox ItemsSource:

var obtenerImagen = (from n in basedeDatos.tablaProductos               
                     select n.imagenProducto);                          

listBox1.ItemsSource = obtenerImagen;

This is code which basically use image from resources of project and put it in button.

<Button>
   <Button.Background>
      <ImageBrush ImageSource="/Application;component/Images/Image.png" />
   </Button.Background>
</Button>

Need Your Help

Darwin libraries validation not yet implemented

ruby-on-rails ruby osx rvm

Trying to setup Rails environment on OS X. When I run rvm reinstall all --force, I get an error message:

Get rid of extra HTMl tags using jQuery

html tags add

When am using dynamic content its creating extra tags how can i get rid of extra HTMl tags using jQuery

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.