C# WPF-对话框相对源绑定(MVVM)

C# WPF-对话框相对源绑定(MVVM),c#,wpf,mvvm,data-binding,C#,Wpf,Mvvm,Data Binding,我有一个WPF对话框,我正在尝试访问所有者的ViewModel中的属性 我尝试了以下操作,但它绑定到对话框的ViewModel: <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Property}"/> 我在StackOverflow中看到了几个问题,但似乎都不起作

我有一个WPF对话框,我正在尝试访问所有者的ViewModel中的属性

我尝试了以下操作,但它绑定到对话框的ViewModel:

<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, 
        AncestorType={x:Type Window}}, Path=DataContext.Property}"/>
我在StackOverflow中看到了几个问题,但似乎都不起作用

有什么想法吗?
在对话框中是否可能有相对源?

如果我在子窗口上显式设置所有者,这对我来说是可行的:

<ComboBox 
    ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=Owner.DataContext.Property}"/>
然后,当您显示子窗口时

var dialog = new Dialog(DialogViewModel)
{
    Owner = Window.GetWindow(this),
    OwnerDataContext = DataContext
};
作为DependencyProperty,绑定将与它正确交互。所有者窗口可以随意设置它,您可以绑定到它。按照绑定的工作方式,它是duck类型的,因此对于
OwnerDataContext
具有声明的对象类型的绑定来说,这一点都不重要。无论如何,这都是反射

<ComboBox 
    ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=OwnerDataContext.Property}"/>


我可能完全错了,为什么你的代码不工作,但我打赌这将在任何情况下工作

wpf+mvvm+Shell started project+多个子模块,当您想在子模块的viewmodel中写入一些数据时,sheel将以dll的形式调用这些数据,这些数据可能被称为子模块A

在SubModuleA中,当您想将数据写入子模块的资源文件时,例如SubModuleA/resource/data/JsonData.json此文件

在子模块的viewmodel中,相对文件路径为


字符串jsonFilePath=“../../../SubModuleA/Resource/JsonData/JsonData.json”

我希望
Owner.DataContext.Property
能够正常工作,就像在第二个示例中添加
.Property
一样。这是一个输入错误,谢谢。很抱歉,我忘记了属性,但它给出了相同的结果。属性是一个带有get/set的属性,而不仅仅是一个字段吗?是的,属性有一个get/set。它可能是上下文之外的,但GetValue()和SetValue()是方法在某个库中,还是它是自定义实现?Thanks@J.Pichardo它们是从DependencyObject IIRC继承的。依赖属性是WPF的一个基本部分。这会给你带来麻烦吗?
#region OwnerDataContext Property
public Object OwnerDataContext
{
    get { return (Object)GetValue(OwnerDataContextProperty); }
    set { SetValue(OwnerDataContextProperty, value); }
}

public static readonly DependencyProperty OwnerDataContextProperty =
    DependencyProperty.Register("OwnerDataContext", typeof(Object), typeof(SubWindow),
        new PropertyMetadata(null));
#endregion OwnerDataContext Property
var dialog = new Dialog(DialogViewModel)
{
    Owner = Window.GetWindow(this),
    OwnerDataContext = DataContext
};
<ComboBox 
    ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=OwnerDataContext.Property}"/>