Вопрос

I followed different tutorials and example and they showed me those different type of binding but I can't understand when to use one and when use another.

For example sometimes I see simply:

Binding="{Binding}"

Sometimes (and this I understand how to use it) after setting the DataContext:

Binding="{Binding Propriety1}"

In DevExpress GridControl i see:

Binding="{Binding Data.ProprietyName}"

And others.
Can one explain shortly and clear why so different cases? I searched online but the tutorials say only what binding is (and I know what is) and a simple example like the third I wrote.

Thank you in advance.

Это было полезно?

Решение

K here's the simple explanation you asked for:

Binding="{Binding}"

that is when you're binding the DataContext object itself. So whatever is the DataContext in the current scope where the binding is what is being bound. that can also be written as Binding="{Binding .}"

Next:

Binding="{Binding Propriety1}"

Here you're binding Propriety1 within the current DataContext. This one is the same as {Binding DataContext.Propriety1, RelativeSource={RelativeSource Self}}" />

Finally:

Binding="{Binding Data.ProprietyName}"

Here you're binding ProprietyName which belongs to Data which is a property declared in the current DataContext

All these are shown in this example: Download Link

<StackPanel x:Name="LayoutRoot">
  <TextBlock DataContext="{Binding TestStringOne}"
              Text="{Binding}" />
  <TextBlock DataContext="{Binding TestStringOne}"
              Text="{Binding .}" />
  <TextBlock Text="{Binding TestStringTwo}" />
  <TextBlock Text="{Binding Data.TestStringThree}" />
</StackPanel>

The properties and Data object's class you can find in MainViewModel.cs for these

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top