Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XAML - Binding to DataContext and using converter?

To bind to the current DataContext in XAML you can use:

<TextBlock Text="{Binding}" />

How do you do this using a converter in the mix? The following works when you have a property on the path:

<TextBlock Text="{Binding MyProperty,Converter={StaticResource converter}}" /> 

But I dont want to do that; I just want to Bind to the datacontext and not the datacontext.MyProperty if you get what I mean.

like image 605
Dan Avatar asked Aug 27 '09 03:08

Dan


People also ask

How do I bind data in XAML?

Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.

What is DataContext in XAML?

The XAML in WPF is just a pretty user interface to display and interact with the actual data, otherwise known as the DataContext . The purpose of other binding sources ( RelativeSource , ElementName , etc) is to point to another property that doesn't exist in the current control's DataContext.

How does WPF binding work?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.

How many types of binding are there in WPF?

WPF binding offers four types of Binding. Remember, Binding runs on UI thread unless otherwise you specify it to run otherwise. OneWay: The target property will listen to the source property being changed and will update itself.


2 Answers

Simply omit the path:

<TextBlock Text="{Binding Converter={StaticResource converter}}" />

Ah wait - I notice your question is tagged with Silverlight. Does this not work in Silverlight? If not, you may need to use the expanded syntax:

<TextBlock>
    <TextBlock.Text>
        <Binding Converter="{StaticResource converter}" />
    </TextBlock.Text>
</TextBlock>
like image 50
Matt Hamilton Avatar answered Oct 16 '22 11:10

Matt Hamilton


Dot sign also provide DataContext Binding for SL developers

<TextBlock Text="{Binding Path=.,Converter={StaticResource converter}}" />
like image 37
Davut Gürbüz Avatar answered Oct 16 '22 12:10

Davut Gürbüz