Image not showing in wpf button
I made a button theme as a resource dictionary and made a sure the content of the button is an image whose source is binded to the constructor of the button when is being made in the application. The problem is the button is not showing. Please i need assistance
83 Replies
this is the resource dictionary of the button in particular
and this is the constructor of the button in my application
This doesn't really make sense.
So here you're binding the Image to Content, sure.
However, here, the
Button.Content
is...another image. So you're setting the Image.Source as an Image control...which isn't a thing.
It seems mostly unnecessary to replace the ControlTemplate
here, but it's been awhile since I used WPF.
You should probably just be using a ContentPresenter
here instead of Image
(or maybe instead of the Grid altogether).I thought you can make the content as an image. I am new to this
You can, by default, because the default control is correctly made.
ContentPresenter
is what you need here to forward the <Button.Content>
into actually being presented.
There's not really a need for a new ControlTemplate
here, IMO. But if you still want it, check out https://github.com/dotnet/wpf/blob/991e7030d569bc6d0480d7126e01a28a9465d05e/src/Microsoft.DotNet.Wpf/src/Themes/XAML/Button.xaml#L160 to see how the button is originally made.
At least the style-sideNow i am confused
Why?
BEcause i dont know what i did wrong. I made the content of the button to be an image in the resource dictionary
So that any picture i add is centre aligned
I read that control template is used to change the visuals of a control
That is why i used it
Here i bound the source of the image to be the content of the button
but the content in the constructor is already an image
And you can't set an
Image.Source
to a Control
. Image.Source
needs an ImageSource
. Conversely, Button.Content
needs a control (well, not exactly...) and not an ImageSource
.
This is why you use ContentPresenter
in your template.I see. So the problem was using a control template to modify the behaviour of the button?
or i should use a content presenter in the control template?
Right. Like the original button style does.
but i dont understand how i can make the content presenter source to be the content of the button
Do i use the SourceUpdated property?
You just bind the
Content
.
You keep the external XAML exactly the same. ie. This part:
to what property
when i try to do that, it tells me that Content is not recognized or accessible
Hmm, this is different in WPF. Maybe it's implicit and
<ContentPresenter />
doesn't need a binding here.
Just follow what the original style does for ContentPresenter
and see what happens.
Though it should have a Content
property.But i dont know how to use content presenter. I have not used it before nor do i know what it is supposed to do. It does have a content property tho
ContentPresenter.Content Property (System.Windows.Controls)
Gets or sets the data used to generate the child elements of a ContentPresenter.
Yeah, it's implicit in WPF.
Did you read the style?
It literally tells you the implementation. Just follow it.
Okay. So you recommend i put the image in the theme and not in the constructor of the button?
That is not what I said at all.
I said:
1. Follow the WPF style which uses
ContentPresenter
2. Keep your external XAML with <Button.Content>
the same.
Do not put an <Image>
control in your template.Hmmm. So something like this?
Yes, but
Content="{TemplateBinding Content}"
is apparently not necessary.Okay i removed it and left my button constructor the way it is, but it is not show still
Ok, lets move one step backwards.
Try showing your image in a normal image control, not within your styled button.
Just something like:
Okay. It shows after i comment
out
That's a different image file than before.
yah i have many i was trying. Before i had a problem when some image files could show
I'm just trying to rule out your image resource settings being incorrect and the image not being located.
But the image is displaying
all the images are in one folder
That doesn't matter. You still need to ensure that individually they have the right Build Action.
Oh okay
WHat does that mean tho
I thought as long as the path is correct, it would display the object
You need a Build Action to include the image as a resource (embedded within the executable) or Copy to Output so it is published to disk relative to the executable.
Else the image just stays in your solution folder doing nothing.
Sorry if i am asking too many questions. I am new to this but how do i ensure this
You right click the image in VS and select properties.
Out-of-the-box, the original Button control style already works with child Image content as long as you're correctly setup.
So you can use that as another check.
so what do i look for in properties. there is not much there
Build Action or Copy to Output.
I think this is fine tho
but there is supposed to be a picture there. dunno why it's not showing
If it shows in the designer and not at runtime, then your images are likely setup incorrectly as I mentioned.
I see. Does rebuiding the solution fix it?
Okay. I put the full path and it works. I'd like to figure out how to set the paths well
No, rebuilding doesn't fix it.
You need to follow what I say.
Klarth
You need a Build Action to include the image as a resource (embedded within the executable) or Copy to Output so it is published to disk relative to the executable.
Quoted by
<@160638834109972480> from #Image not showing in wpf button (click here)
React with ❌ to remove this embed.
But like how do i get a build action
I dont find that in the image property
You click it? You should have something like this (don't mind the AvaloniaResource)
nope not there
Do i need a nuget package like avalonia?
Don't mind Avalonia. It's a different framework. I just don't have a WPF project open.
okay. but what do i do. I dont have that build action property
I'm trying to figure out why you don't have the extra options.
Thank you sir for sticking with me. I really appreciate it
Is this a .NET or .NET Framework project?
.NET framework. For some reason there were no WPF controls in the .NET project
FYI, nobody cares about the drag/drop designer in WPF. It leads to bad layout code. Devs write XAML by hand and use autoformat tools like XAML Styler.
I found that out. That was two days ago but i wasnt sure at the moment
I actually only got into .NET on monday
And i have learnt a lot in that time
But still have a lot to learn
Yeah, it's fine for now.
but i'm still confused about .NET and framework
Eventually, you'll want to migrate and it's a pain.
Well, there's an Upgrade Assistant tool now which works decently well. But before, you had to migrate csproj by hand.
So your're better off developing on .NET right?
Not really sure what to say. Pops up for me in a .NET Framework project.
Maybe it has to do with git
.NET is like 7+ years ahead of .NET Framework in terms of API/features/performance.
I pushed changes i made yesterday when i hit a milestone but the images werent there yet
.NET Framework only has the advantage of being preinstalled on Windows (and smaller binaries because of that).
they have these weird icons
I can't remember exactly what that icon means.
Either it exists but is not included in the project...or it's reference by the project but doesn't exist on disk.
What I would do is: remove the images from the project and re-import them.
Hmm. So basically delete them and copy them back in?
still the same
maybe i try to push the image folder to my githug repo?
How are you importing them?
https://stackoverflow.com/questions/38464552/what-is-the-meaning-of-the-dotted-file-icon-in-visual-studio-2015-solution-explo
So the first one then...exists but not included in the project.
omg
You should be able to do this
I just added the pictures from the need to the install folder
I have just included it
.NET doesn't have this issue. This is an issue with the old csproj that the .NET Framework uses.
How to i migrate to .NET
Either run Upgrade Assistant or recreate project.
It's a pain I don't have the time to walk through.
Understandable. Which would you recommend
Probably recreate. You don't have that much in terms of files. Probably no breaking APIs either.
Hmm. Okay. I really appreciate your help and everything to be very honest. I have one more favour but i dont know if i should ask
Go for it.
You seem to know a lot about .NET and you use Avalonia which i heard is for season vets. I was wondering if you had time to mentor 🥺 . I really want to be a fullstack .NET dev
It would be a really great help to haev someone to learn from
I probably don't have the time for that, TBH. Myself and others are around in #gui to answer questions though. In general, there are a lot of professional devs in the server with much more experience than I have.
alright. I appreciate the help though. I will do more research and laern .NET