Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] Properly implementing WPF flat buttons

Started by CGameProgrammer May 11, 2010 at 3:28 PM 2 replies 2.5k views
Original Post
CGameProgrammer
CGameProgrammer
A much-discussed topic online, one method I found was to use negative numbers for padding to effectively erase the border region. With an image of 50x50 where every pixel must show up exactly (1:1), a way I've found to create a flat button using that image, shown exactly 50x50, is:

         BitmapImage bitmap = new BitmapImage( new Uri("BtnImage.png", System.UriKind.Relative) );
         Image image = new Image( );
         image.Source = bitmap;

         Button button = new Button( );
         button.Width = 50;
         button.Height = 50;
         button.Background = Brushes.Transparent;
         button.BorderBrush = Brushes.Transparent;
         button.HorizontalContentAlignment = HorizontalAlignment.Stretch;
         button.Padding = new Thickness(-3);
         button.Content = image;
It so happens that a padding of -3 exactly erases the border region and results in a 50x50 image button. My question is, is this reliable or would it appear broken on some people's machines for whatever reason? It sure seems like a hack to me.
~CGameProgrammer( ); Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
ChaosEngine
ChaosEngine
That looks like it's dependant on a specific button template. The templates change based on the UI theme (i.e. classic windows, XP luna, or vista/7 aero).

Also, don't use an image for your button. Your button won't provide any visual feedback for mouse over or pressed states, and it's inflexible (you can't easily change the size). WPF provides pretty powerful xaml based drawing, use that instead.

If you want to completely change the buttons appearance I'd recommend writing your own button template.

Post a picture of what you want to achieve and I might be able to help you.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
CGameProgrammer
CGameProgrammer
It was a small 10x10 image button and I use a different image on mouse-over. I decided to just make it a generic UserControl since that already supports what I needed (aside from a mouse-click event which I just implemented myself).
~CGameProgrammer( ); Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Side Winder
Side Winder
Can't you change the ControlTemplate to change this?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.