下面的代码有效,但我很好奇为什么我需要Path以"DataContext"作为前缀?在大多数其他情况下,使用的路径是相对于DataContext的.是因为我使用的是RelativeSource吗?因为源是根级别(Window)?
<Style TargetType="TextBox">
<Setter
Property="IsReadOnly"
Value="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}}, Path=DataContext.IsReadOnly}"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
Ree*_*sey 13
您绑定到包含Window的DataContext,而不是绑定到Window本身.你要放:
Value="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}}, Path=IsReadOnly}"
Run Code Online (Sandbox Code Playgroud)
这将绑定到IsReadOnlyWindow 的属性,而不是其数据上下文类.由于Window不包含IsReadOnly属性,因此显然来自不同的类(很可能是ViewModel,如果您使用的是MVVM等).
| 归档时间: |
|
| 查看次数: |
12684 次 |
| 最近记录: |