Binding Basics Tutorial | WPF
Learn one of WPF’s key features - Binding!
Subscribe! ► https://www.youtube.com/c/QuasimodoPrograms?sub_confirmation=1
You may have seen this word when you worked with a datagrid in Windows Forms. Do you remember that miracle when you clicked a cell in the datagrid and the textboxes showed you the data from the line?
Well, in WPF, Binding is a powerful programming tool used all the time. Actually, you can build WPF programs even without it but believe me, this thing will save you a lot of time and lines of code.
Binding implies the use of two objects: a source and a receiver. The receiver binds to a certain property of the source. If that property of the source changes, the receiver will know about it automatically. For example, you can bind the Text property of a TextBlock to the Text property of a TextBox.
In this example, the TextBox will be defined as usual. But the TextBlock needs a special expression in its Text property. The most basic format is as follows: an opening curly brace, type “Binding”, whitespace, ElementName, equals, the name of the source, comma, whitespace, Path, equals, the property of the source, and a closing curly brace:
= {Binding ElementName=tblc, Path=Text}
Now, if we type in the TextBox, the text of the TextBlock changes as well.
So, Binding is a class which has its own properties. We have already used 2 of them: ElementName and Path. There are other properties like Mode, RelativeSource, Source and so on.
The Mode property can be set to OneWay, which means that the property of the receiver changes after the source property changes. It can be set to OneTime, then the receiver property is set to the source property only once and does not track the source afterwards. Mode can be set to TwoWay, when both the source and the target can affect each other. It can be set to OneWayToSource, which means that only the receiver changes the source. And finally, Default. It means TwoWay for TextBox.Text, but OneWay for everything else.
When you set OneWay, changes are immediate, but when you have TwoWay or OneWayToSource, changes take place only after the TextBox loses its focus. This behavior is decided by the UpdateSourceTrigger property of Binding. It can take one of 4 values: PropertyChanged means the source gets updated right after the receiver; LostFocus means the source is updated after the receiver’s property is changed; Explicit which implies calling a BindingExpression from code; and Default which is PropertyChanged for most properties but LostFocus for TextBox.Text.
In case the bound property is not set, you can use the TargetNullValue property of Binding to show some default value. This is the case when you bind to null public properties in C# code.
Binding can also work even with controls without a name by using the RelativeSource property. And again, you use the same name object RelativeSource and set its Mode property. Here mode can be Self or FindAncestor. The latter binds to the property of the container. Let’s bind TextBox.Text to its Background property.
And let’s bind to the grid property inside which the TextBox is placed. If we use FindAncestor, we also must specify AncestorType.
You can also bind to public properties in your C# code, but this is a little out of scope, since here I try to concentrate only on XAML.
Well, that’s it for Binding basics. Thanks for watching.
If you want to know how to bind to public properties in your C# code, check https://youtu.be/R-E04ZjGEfk
Latest code: https://github.com/QuasimodoPrograms/WPF-Basics/tree/main/BindingBasics
More WPF Basics ► https://youtube.com/playlist?list=PL3g0PHY_11p5IVMSXwk5J4tIiB14K11TV
Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «Binding Basics Tutorial | WPF», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.
Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.
Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!
Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.