1

I am a newbie to xaml and windows app dev so sorry if this question might seem silly.

I created a textbox and in the designer I right clicked it and selected edit template=>edit a copy and put it into my custom dictionary.

In the control template for this textbox I saw visual states like disabled,focused and so on. And I modified them and run the mobile app and observed that my changes work like changing border color when textbox is focused.

But in order for this to work somebody has to call

VisualStateManager.GoToState("Focused")

when the textbox is focused so who is calling this because I don't see any visual transitions in the control template so how is this happening?

Mohit Shah
  • 843
  • 1
  • 6
  • 20

2 Answers2

1

XAML is a compiled language, and if you've looked extra close, what happens under the hood, is that the class behind your xaml has the same namespace as your xaml code.

This means (for no practical purpose) that compiling your program turns all of that XAML into C# code before then going over to MSIL and eventually execute as a binary program.

Much of the state changes that happen are event based, and TextBox, like all other user controls, will transmit a message and listen to messages. The Page that contains the TextBox will probably be the one that transmits a state change whenever one of it's children gets focus, and as a good control, the TextBox listens for this event and reacts to it.

Pedro G. Dias
  • 3,162
  • 1
  • 18
  • 30
  • But if I want to create a custom template and transmit these messages how will I do so?? – Mohit Shah Apr 01 '16 at 05:19
  • The custom template, like all other XAML, is compiled. A custom template is nothing else than a mix of already existing controls, and all of them have their messaging plumbing already working for you – Pedro G. Dias Apr 01 '16 at 05:22
  • no supposing I want that when my button gets clicked and the background color of button changes and I want to implement all this in xaml using the messaging technique u said how to do so? – Mohit Shah Apr 01 '16 at 05:23
  • I don't want to write c# just do this in xaml how then? – Mohit Shah Apr 01 '16 at 05:24
  • There is a similar example doing this for changing the image on a button whenever the mouse hovers over it, it will give you some ideas, I hope: http://stackoverflow.com/questions/10524508/change-button-image-on-hover-or-click – Pedro G. Dias Apr 01 '16 at 05:25
  • Just one last thing If I am creating my custom control and custom template then how will I implement this messaging mechanism? Because in the link it shows editing a template for a predefined xaml control – Mohit Shah Apr 01 '16 at 05:29
  • The custom control will be in a namespace, so if you just include your namespace where you need to use it, you typically import that namespace using the xmlns: thing at the top of the page, for example xmlns:myControls="your.namespace.here" and then use that control such as – Pedro G. Dias Apr 01 '16 at 05:31
1

The code in the control itself is calling VisualStateManager.GoToState(...) .

When you start implementing your own custom controls, you might subscribe to events you have available and transition states based on your own logic. Here is an example of a custom control with its own two custom states.

https://github.com/xyzzer/WinRTXamlToolkit/blob/master/WinRTXamlToolkit/Controls/WatermarkTextBox/WatermarkTextBox.cs

foson
  • 10,037
  • 2
  • 35
  • 53