Alexey Markov
Alexey Markov

Reputation: 1566

Binding to a nested property in template

I want to create a button with loading indicator for async methods, like this:

enter image description here

I create an AsyncCommand implementing ICommand and INotifyPropertyChanged(more details in MSDN Magazine April, 2014).

My AsyncCommand has Execution property and it has IsNotCompleated property. I want to set Visibility property of loader indincator using IsNotCompleted property.

I create UserControl inherits the Button and set style for this. I try to bind visibility to Button's Command dependency property.

There the part of ControlTemplate with "Loading indicator" (I use content, not Visibility, just for example)

 <Label HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Grid.Column="1"
        Content="{Binding Command.Execution.IsNotCompleted}"/>

And nothing happens. If i use TemplateBinding instead of Binding I'll get compile errors:

What is the correct way to do this?

Upvotes: 2

Views: 758

Answers (1)

Alexey Markov
Alexey Markov

Reputation: 1566

The solution is using RelativeSource.TemplatedParent

<Label HorizontalAlignment="Center"
    VerticalAlignment="Center"
    Grid.Column="1"
    Content="{Binding Command.Execution.IsNotCompleted, RelativeSource={RelativeSource TemplatedParent}}"/>

Upvotes: 3

Related Questions