使用Window的RelativeSource进行WPF绑定需要Path中的“DataContext”吗?

前端之家收集整理的这篇文章主要介绍了使用Window的RelativeSource进行WPF绑定需要Path中的“DataContext”吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
下面的代码有效,但我很好奇为什么我需要Path以“DataContext”作为前缀?在大多数其他情况下,使用的路径是相对于DataContext的.是因为我使用的是RelativeSource吗?因为源是根级别(Window)?
<Style TargetType="TextBox">
        <Setter 
           Property="IsReadOnly"
           Value="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}},Path=DataContext.IsReadOnly}"/>
    </Style>
您绑定到包含Window的DataContext,而不是Window本身.你是这样说的:
Value="{Binding RelativeSource={RelativeSource FindAncestor,Path=IsReadOnly}"

这将绑定到Window的IsReadOnly属性,而不是其数据上下文类.从Window doesn’t contain an IsReadOnly property开始,这显然来自不同的类(很可能是你的viewmodel,如果你使用的是MVVM等).

原文链接:https://www.f2er.com/windows/365430.html

猜你在找的Windows相关文章