C# – WPF – Runtime and Command Binding of Labels

In the current article I will show how to bind elements in a small application. Two types of binding are presented – the so called (by me) runtime binding and command binding. The difference is that the runtime binding is carried out automatically, without a specific command and the command binding is initiated by an event (in our case of a selection of a checkbox). So, we have the following:

  • One Text Box
  • Two Labels
  • Two Check Buttons

What do we do with these? 🙂

We make sure, that the first label writes automatically the strings, entered in the text box. It is not that difficult and this is achieved with programming from the XAML code:

With these lines, we inform the Label, which I have named “lb3”, that it should display the Text from the element “tb”. This is done through the Content property of the label. This is what I understand under “Runtime Binding”, because this binding works after the start of the application automatically as follows:

Binding

 

The more interesting part is the so-called “Command Binding” (the term is coind by me 🙂 ). It binds only if you select the checkbox “Binding”. In this case the method “cb_Checked” is initiated. In order to make the example a little more interesting, I have added colors at random (absolutely), which are run once you decide to “Bind” the application. The colors can be disabled (or simply changed to black and white) once you click on “Disable Colors” checkbox. As in the previous example, the colors are defined as styles in the XAML code and are accessed from C#.

OK, here is the the XAML code:

Here comes the C# code:

Enjoy the code. If you want to download the application, you may do so from here.

Tagged with: , , , ,