Converter Basics

So, you have your first XAML page up. Well done.

Let’s say your manager comes over and says that now when a user uses the app, if they are Canadian the app should show that they now get free Moose Butter.

Let’s have a look at one way we can skin this Moose. On a high level, your Model could have a new property that you could bind to the Page, perhaps a Label like this:

2017-09-07 14_08_49-CoApp - Microsoft Visual Studio  (Administrator).png

But that is not very re-usable. Instead we can simply ask the Model if the user is a Canadian and if so, show that text.

First we need a converter which will understand “Canada” as a country and the text to show.

2017-09-07 14_11_03-CoApp - Microsoft Visual Studio  (Administrator).png

It has two simple functions. We’ll just concentrate on the conversion of text “Convert”, not back and forth with “ConvertBack” It’s one way.

Now let’s add it to our form so that a Label can use it:

2017-09-07 14_13_17-CoApp - Microsoft Visual Studio  (Administrator).png

Then we can consume it:

2017-09-07 14_14_05-CoApp - Microsoft Visual Studio  (Administrator).png

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s