Olaru Mircea
Olaru Mircea

Reputation: 2620

WPF Binding two dependency properties of the same control but two datacontexts needed

I have two controls in a grid.

   <TextBlock Text="{Binding Name}" TextAlignment="Center" />
   <TextBox Visibility="{Binding ElementName=EditMode,Source={Binding RelativeSource=
                   {RelativeSource FindAncestor, AncestorType={x:Type Window}}}, 
                   Converter={StaticResource BoolToVis}}"  Text="{Binding Name}" 
                   TextAlignment="Center" />

I am trying to implement something like an editable/non editable behaviour. I know i might choose for a TextBox and simply change the IsEditable property but still, in my scenarion i would need to DataContext, at least that's what i am thinking of.

In my example, the TextBlock works fine, and the Text property on the TextBox also works fine but for the Visibility part, i want to bind to a data property ( EditMode which is a boolean) found on some other layer. Is there a way to change the DataContext to that, but only for the Visibility ? The Text property should remain as it is now.

Should i try a hack, to define an invisible checkbox, change IsChecked when my Edit button is clicked and bind directly to that ? I will try this. I think this way, no DataContext changing is needed.

Upvotes: 0

Views: 574

Answers (2)

Derrick Moeller
Derrick Moeller

Reputation: 4970

It seems to me you were almost there, you should be able to use a RelativeSource to do this. The issue is that you misused ElementName, ElementName is to bind to a property of a named source and would be used instead of RelativeSource. What you meant to use was Path, which is optional as seen below.

<TextBox Visibility="{Binding DataContext.EditMode, RelativeSource={RelativeSource AncestorType={x:Type Window}}, 
               Converter={StaticResource BoolToVis}}"
         Text="{Binding Name}" TextAlignment="Center" />

Upvotes: 1

Sheridan
Sheridan

Reputation: 69985

@FrumRoll is correct that you can access a property that is not in the set DataContext object using a RelativeSource Binding. However, I'm not sure that their code is quite right... try this:

<TextBox Visibility="{Binding DataContext.EditMode, RelativeSource={RelativeSource 
    AncestorType={x:Type YourXamlPrefix:MainWindow}}}, Converter={StaticResource 
    BoolToVis}}" Text="{Binding Name}" TextAlignment="Center" />

Clearly, you'll need to change the YourXamlPrefix value with your local XAML Namespace Prefix and MainWindow with the name/type of your Window if it is not called MainWindow. This also assumes that your EditMode property has been defined in that Window.

This may also work, but is not specifically looking for your exact Window, so may have some problems:

<TextBox Visibility="{Binding DataContext.EditMode, RelativeSource={RelativeSource 
    AncestorType={x:Type Window}}}, Converter={StaticResource BoolToVis}}" 
    Text="{Binding Name}" TextAlignment="Center" />

Upvotes: 1

Related Questions