AlexSJC
发布于 2024-08-31 / 15 阅读
0
0

WPF MVVM 中 ItemsControl 内绑定外层 DataContext

方法一:通过 ElementName 绑定外层控件的 DataContext

{Binding DataContext.×××, ElementName=×××}

方法二:通过 RelativeSource 绑定外层控件的 DataContext

{Binding DataContext.×××, RelativeSource={RelativeSource AncestorType=×××}}

方法三(推荐):将 ViewModel 作为资源并用 Source 指定。

<×××.Resources>
    <×××ViewModel x:Key="×××ViewModel"/>
</×××.Resources>
{Binding ×××, Source={StaticResource ×××ViewModel}}

https://learn.microsoft.com/zh-cn/dotnet/desktop/wpf/data/how-to-specify-the-binding-source

https://www.cnblogs.com/yzw123/p/15599092.html

https://blog.csdn.net/weixin_44246065/article/details/104723561

https://bbs.csdn.net/topics/392291709


评论