views:

132

answers:

1

The following code works, but I'm curious as to why I need the Path to be prefixed with "DataContext"? In most other cases, the path used is relative to DataContext. Is it because I am using a RelativeSource? Because the source is at the root level (Window)?

    <Style TargetType="TextBox">
        <Setter 
           Property="IsReadOnly"
           Value="{Binding RelativeSource={RelativeSource FindAncestor, 
           AncestorType={x:Type Window}}, Path=DataContext.IsReadOnly}"/>
    </Style>        
+2  A: 

You're binding to the containing Window's DataContext, not to the Window itself. Were you to put:

Value="{Binding RelativeSource={RelativeSource FindAncestor, 
       AncestorType={x:Type Window}}, Path=IsReadOnly}"

This would bind to the IsReadOnly property of the Window, not it's data context class. Since Window doesn't contain an IsReadOnly property, this is obviously from a different class (most likely your ViewModel, if you're using MVVM, etc).

Reed Copsey
Makes sense. Thanks.
Phil Sandler