# .NET and More > WPF, WCF, WF >  Trying to set button image programmatically

## szlamany

I've got this code that creates a list of buttons to put into a toolbar strip.

I am putting a StackPanel in for the Content of the buttons - and I am seeing the TEXTBLOCK but not the image.

Any ideas why this might be?



```
            List<Button> list = new List<Button>();
            list.Clear();
            Button ib = new Button();
            ib.Style = (Style)FindResource("OptionButtonStyle");
            ib.Tag = "1";
            ib.Background = buttonOff;

            StackPanel sp = new StackPanel();
            sp.Orientation = Orientation.Horizontal;
            System.Windows.Controls.Image img = new System.Windows.Controls.Image();
            img.Height = 16;
            img.Width = 16;
            img.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            img.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            img.Source = (ImageSource)FindResource("ExplorerViewImage");
            sp.Children.Add(img);
            TextBlock tb = new TextBlock();
            tb.Text = "Explorer View";
            sp.Children.Add(tb);

            ib.Content = sp; // "Explorer View";
            ib.ToolTip = "Show Explorer View";
            list.Add(ib);
```

----------


## dee-u

Well, this works for me:



```
List<Button> list = new List<Button>();
list.Clear();
Button ib = new Button();
//ib.Style = (Style)FindResource("OptionButtonStyle");
ib.Tag = "1";
//ib.Background = buttonOff;

StackPanel sp = new StackPanel();
sp.Orientation = Orientation.Horizontal;
System.Windows.Controls.Image img = new System.Windows.Controls.Image();
img.Height = 16;
img.Width = 16;
img.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
img.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;

BitmapImage image = new BitmapImage(new Uri("/TestWPF;component/Images/acrobat.ico", UriKind.Relative));

img.Source = image;
sp.Children.Add(img);
TextBlock tb = new TextBlock();
tb.Text = "Test";
sp.Children.Add(tb);

ib.Content = sp; // "Explorer View";
ib.ToolTip = "Test";
list.Add(ib);

toolBar1.Items.Add(ib);
```

How are you adding the buttons to the toolbar?

----------


## szlamany

I changed it to this and it worked



```
            BitmapImage image = new BitmapImage(new Uri("/Images/Explorer View1.png", UriKind.Relative));

            img.Source = image;
```

My resource was setup like this



```
        <BitmapImage x:Key="ExplorerViewImage" UriSource="../dfx/Images/Explorer View1.png" />
        <BitmapImage x:Key="FolderViewImage" UriSource="../dfx/Images/FolderView 64x64.png" />
```

Why would they not work?

----------


## dee-u

Tried your code with FindResource and it also worked for me, not sure where the problem is, is the img.Source the only line you changed to make it work?

----------


## szlamany

What did you have for your RESOURCE XAML?  This is what I was trying



```
        <BitmapImage x:Key="ExplorerViewImage" UriSource="../dfx/Images/Explorer View1.png" />
        <BitmapImage x:Key="FolderViewImage" UriSource="../dfx/Images/FolderView 64x64.png" />
    </Window.Resources>
```

----------


## dee-u

I copy pasted your xaml code for that BitmapImage resource and just used my own picture for the UriSource.

----------

