-
Feb 5th, 2014, 04:05 PM
#1
Trying to set button image programmatically
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?
Code:
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);
-
Feb 6th, 2014, 02:13 AM
#2
Re: Trying to set button image programmatically
Well, this works for me:
Code:
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?
-
Feb 6th, 2014, 05:08 AM
#3
Re: Trying to set button image programmatically
I changed it to this and it worked
Code:
BitmapImage image = new BitmapImage(new Uri("/Images/Explorer View1.png", UriKind.Relative));
img.Source = image;
My resource was setup like this
Code:
<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?
-
Feb 8th, 2014, 01:15 AM
#4
Re: Trying to set button image programmatically
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?
-
Feb 8th, 2014, 07:26 AM
#5
Re: Trying to set button image programmatically
What did you have for your RESOURCE XAML? This is what I was trying
Code:
<BitmapImage x:Key="ExplorerViewImage" UriSource="../dfx/Images/Explorer View1.png" />
<BitmapImage x:Key="FolderViewImage" UriSource="../dfx/Images/FolderView 64x64.png" />
</Window.Resources>
-
Feb 9th, 2014, 10:07 PM
#6
Re: Trying to set button image programmatically
I copy pasted your xaml code for that BitmapImage resource and just used my own picture for the UriSource.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|