Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
VB
Private Sub MakeButton(sp As StackPanel, sStr As String)
    Dim btn As New Button()
    Dim sp1 As New StackPanel()
    Dim img As System.Drawing.Bitmap

    img = My.Resources.FldrClose

    sp1.Orientation = Orientation.Horizontal

    'Wrong
    sp1.Children.Add(New Image With {.Source = img})


    'sp1.Children.Add(New Image With {.Source = New BitmapImage(New Uri("c:\tmp\FldrOpen.png")),
    '                                 .Width = 100,
    '                                 .HorizontalAlignment = Windows.HorizontalAlignment.Left})


    sp1.Children.Add(New TextBlock With {.Text = sStr,
                                         .Width = 100,
                                         .HorizontalAlignment = Windows.HorizontalAlignment.Right})

    btn.Height = 30
    btn.Width = 200
    btn.Content = sp1

    sp.Children.Add(btn)
End Sub


In Resources I have several "png" files. This "png" files I want to add to my buttons.
Posted
Updated 22-May-13 5:13am
v2
Comments
Pheonyx 22-May-13 10:50am    
How do you mean? Do you want to see both the string and the image?
If so, why do you want to do it in code and not XAML ?
quator 23-May-13 3:22am    
In the button, I want an image file and a string. Depending on the situation there will be an image and a string in the button. And because I do not know in advance what string comes in the button I want to do this through the code. The string comes from the database.

But how do you add an image to a button of the Resources. Is there a another way, a must have two kind of image in the button.

1 solution

I would advise reading about datatemplates. I have not found an easy way to dynamically put an image and text on a button in WPF, generally the way to do it is to create the placeholders in XAML.
So your button would be described something like this:

XML
<button>
    <grid>
        <grid.rowdefinitions>
            <rowdefinition />
            <rowdefinition />
        </grid.rowdefinitions>
        <image grid.row="1" x:name="ImagePlaceHolder" xmlns:x="#unknown" />
        <textblock grid.row="2" x:name="TextPlaceHolder" xmlns:x="#unknown" />
    </grid>
</button>


Then you could refer to those controls at runtime.

However, a better solution would be to have two dataclasses

C#
ButtonContentWithImage


C#
ButtonContentWithoutImage


Then set the datacontext of the button to one of those classes and use Datatemplates in the resources of the WPF window/control/page to display them properly.

Have a read on some of these links:

http://stackoverflow.com/questions/1933127/creating-an-imagetext-button-with-a-control-template[^]
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1f4b6cf8-fe34-43be-bef7-e01c2a4f1b04[^]

http://wpftutorial.net/DataTemplates.html[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900