Results 1 to 6 of 6

Thread: Trying to set button image programmatically

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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);

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    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?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    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?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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>

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    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.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width