0

First of all, I'm new to XAML / C# (am iOS/Android programmer) so please bear with me instead of immediately voting down. My app has some GridViews that contain buttons. A lot of these buttons are similar and I would like to refactor this. For simplicity let's say my buttons are just Rectangles with a given color. This color might come from the Item class that defines the particular item in the GridView, or it might be hardcoded. I want the rectangle to change color on hover and pressed states. I want these colors to be parameters as well.

What is the best way to achieve this?

  1. I tried to make a Button subclass but somehow I couldn't access the dependency properties in the VisualStateManager
  2. I tried to write stuff in the code-behind but then I wasn't sure how to delegate the click command to the ViewModel class.

Could someone give me a small working example?

Thanks

2
  • Sounds like what you want is to define a button style. msdn.microsoft.com/en-us/library/bb613545.aspx Commented Dec 30, 2012 at 2:45
  • I think I know how to create a style for the button. The main point is that I want to be able to parametrize them as follows: <Controls:MyButton Rectangle1Coords="20,20,40,40" Rectangle1Color="Blue" Rectangle2Coords="50,50,60,60" Rectangle2Color="Green"/> where those parameters are all custom dependecy properties... Commented Dec 30, 2012 at 3:04

2 Answers 2

4

You can do this with style templates.

In the Visual Studio designer, right-click on your button and then select Edit Template and then select Edit a Copy....

Button Template Edit

You will then be prompted to name your new style and also for which file to store it in. For now, just give it a unique name such as MyButtonStyle, and select the current file.

Style Creation Popup

Visual Studio will then add a copy of the style to the current xaml document, and will update your button to use the new style.

<Button x:Name="Download" Style="{StaticResource MyButtonStyle}"></Button>

After this, you can update the new style including changing the colors for the different visual states such as hover or clicked.

You can then proceed to use the new style in other buttons in the same document. To use the style in multiple xaml documents, you have to pull it out into a common resource file.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Chue, Thanks for your detailed answer. However, my actual buttons with consistent of several rectangles whose coordinates I want to pass as parameters as well. It will also contain an icon. I don't think that's possible with the Templates correct?
@JorisWeimar - the icon should be possible. For the rectangles I'm not sure, but I would take a look at binding: msdn.microsoft.com/en-us/library/windows/apps/xaml/…
0

So you want to adjust your button using custom properties. This is a good time to use a custom control. You can create whatever dependency properties you want and adjust your layout in your code. http://timheuer.com/blog/archive/2012/03/07/creating-custom-controls-for-metro-style-apps.aspx

1 Comment

but i can't seem to access the dependency properties in the visualstatemanager. i need to change the button state depending on presssed, hover, normal...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.