WPF buttons
Hi all, I am very new to wpf c# or just coding in general.
I am trying to recreate these kind of buttons (To be exact I just want the rounded corners the image in the aligned in the center and just a single caption underneath like the WPF GitHub)
I have this for my code but it is generating a lot of issues and also doesn't apply the corners or semi transparent background
This is my XAML
<Button Margin="2 HorizontalAlignment="Center"">
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5"/>
</Style>
</Button.Resources>
<Image Source="/Images/cmd.png" />
<TextBox Text="Device 1"/>
</Button>

53 Replies
I had a quick play, got this:

Good solution i had this in mind but he needs to make it a custom control to make it reusable and should replace rectangle with image
Yeah of course, and add styling for mouse-over
But it's as much effort as I wanted to put into it š
I know
It's good
He just needs a few dependency properties and make the control inherit from button and it will be fantastic
Eh I wouldn't inherit from Button. WPF would normally prefer that you wrap things than subclass them
How is he going to get the click event and command though ?
HMO could you give me a hand on how to make it reusable
as i need to make like 50 of these
and thank you very much canton7
Use the custom control template and paste the template canton gave you in it

ok
i'll give that a go right now
@HMO when i use that custom control it creates a .cs file
am i meant to create a User Control instead ?
Go to the theme folder
It has generic.xaml
Sorry please bare with me, where is the theme folder you are talking about as i can't see it
It's generated in the vs solution
You'll find it quick

when i created it just creates the CustomButton.cs
I got bored: https://paste.mod.gg/dildpktmnooi/2
BlazeBin - dildpktmnooi
A tool for sharing your source code with the world!

There's no mouse-over style etc though, of course
Nice i love your solution
:meowheart:
Bare with me im just implementing it now
and thank you both
(The stuff with
ElementName=root
is a hack, but it's neater than the equivalent RelativeSource
noise)forgive me but i am still struggling to get it going
just working through a few errors
So i have put it in a CustomControl folder
I have adapted your code and im getting this error
The type 'CustomControls:FancyButton' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
What code produces that error?
Where do you define
CustomControls
?BlazeBin - xyuviltzxbhw
A tool for sharing your source code with the world!
xmlns:controls="clr-namespace:DH.CustomControls"
So your namespace is called controls
So, <controls:FancyButton ...
Can I suggest taking a basic WPF tutorial first? You really need to have the basics nailed before starting the more advanced stuffabsolutely. I have been trying to follow some basic tutorials, but the youtube videos always skip over the important details.
If you could recommend a website or something i'd love to look at it
Unfortunately I don't have any good recommendations as I learnt from a colleague
I see
as for the namespace of Control? I thought i changed it to CustomControl
namespace DH.CustomControls
Windows Presentation Foundation for .NET documentation
Learn about using Windows Presentation Foundation (WPF), an open-source, graphical user interface for Windows, on .NET.
YouTube
SingletonSean
Hi, Iām Sean! Iām a software developer with a long history of .NET development (mostly desktop). Outside of the .NET world, I have a huge passion for building web applications and I use Neovim btw.
This channel is focused on .NET desktop development and general software development topics. I try to present topics in a practical and entertai...
These 2 are good enough
Oh i got it
except now its building with 0 errors ? yet coming up with an error
What error?
i don't really know its not showing me
Build started at 9:51 PM...
1>Error: Illegal characters in path.
theres no output errors
what does that mean illegal characters in path, i haven't changed anything ffs
One of your filenames contains illegal characters?
@canton7 I have had a semi victory

this is what its created
Not bad!
Left-align that image, put a colored background on the control behind to see the transparency
am i missing the border?
Nah i want it centered like that, its perfect

there we go
Might want to fix your icon š
Was there a border before? I can't tell. You can stick on one if you want
yeah i love the fact its meant to be transparent
and yet the transparent background isn't
LOL
Thank you again
Something simple would just be:
oh for mouseover
sounds grand
does that mouseover just go in the same FancyButton.xaml file
@canton7 how do i append that to the FancyButton. As i get errors when trying to copy paste that in. I get that it can have only one child. So im wondering do i put that elsewhere ? like usage ?
The first line is a modification of the Border line that's in my original sample. The lines underneath are inserted, before the
<Grid
Yeah but is there a way to make that a template also as i cant append it to your FancyButton
nvm
i think i got it
you are a godsend

in action