Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Binding - Self Binding with its own DataContext

Tags:

binding

wpf

xaml

Anyone got a situation to bind the same DataContext to Text property (for example) in TextBlock.

I have to assign the DataContext to reflect some trigger based on the Data values from Datacontext in my style. at the same time, i need to bind with the same DataContext object to get the Text Property After applying some conversion on either IValueConverter/IMultivalueConverter.

As i know {Binding}, just bind with the current datacontext. But in the same scenario how to use converter with it? Any suggestions will be appreciated.

    <TextBlock Style="{StaticResource DataEntryTextBlock1}" Grid.Row="1"
               DataContext="{Binding MyField1}"
               Text="{Binding MyField1, Converter={StaticResource myConverter}}">
    </TextBlock>

This XAML script does not work, as the Text binding is trying to look for the MyField1 variable inside the MyField1.

Thanks, Vinodh

like image 472
vinodh Avatar asked Mar 20 '23 11:03

vinodh


1 Answers

{Binding} is equivalent to {Binding Path=.} so in you case you can use

Text="{Binding Path=., Converter={StaticResource myConverter}}"

Binding.Path on MSDN

Optionally, a period (.) path can be used to bind to the current source. For example, Text="{Binding}" is equivalent to Text="{Binding Path=.}"

like image 134
dkozl Avatar answered Apr 02 '23 09:04

dkozl