2010-07-23 57 views
2

我是新来WPF和MVVM模式,所以我有一些问题,我的绑定。MVVM中没有静态资源的RelativeSource FindAncestor?

在客户的详细视图中,我想列出组合框中的一些状态。

在我的ViewModel中,客户处于根级,状态列表也如此。

当使用一个静态的资源,我可以使用:

ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}, Path=DataContext.PartGruppAll}" 

在我的组合框,但是当我从代码中设置的DataContext的后面,这是行不通的,我在做什么错,在我看来,应该没有区别。

最好的问候, 彼得·拉尔森

+0

或许能给我们的视图模型代码。它可能有帮助 – aqwert 2010-07-23 07:45:37

回答

0

它不会因为你可能有一个拼写错误

Path=DataContext.PartGruppAll 

威力应该是

Path=DataContext.PartGroupAll 
+0

不,对不起,这个名字中有一部分瑞典语,实际名称是PartGruppAll。 :) – 2010-07-23 07:50:59

+0

好的,不用担心...只是一个预感 – aqwert 2010-07-23 08:00:08

0

噢一件事...

如果StackPanel与组合框t的视觉树相同如果你不需要在绑定中找到它

ItemsSource="{Binding PartGruppAll}" 

应该在DataCoxtext的可视化树中搜索时工作。

+0

不,这也行不通。 – 2010-07-23 11:05:20

0

我会试着给你更多的细节,viewmodel是相当大的,所以我会尽量缩短它。

我实例化视图模型在我的代码背后的App.xaml

 protected override void OnStartup(StartupEventArgs e) 
    { 
     base.OnStartup(e); 

     PartWindow pw = new PartWindow(); 


     var PartViewModel = new ViewModel.PartWindowViewModel(); 
     pw.DataContext = PartViewModel; 

     pw.Show(); 
    } 

然后在我的网页我的数据绑定到一个StackPanel:

<StackPanel DataContext="{Binding Path=PartViewModel}"> 

我然后在网格中显示客户通过绑定到Customer-属性SelectedPart。

<Grid DataContext="{Binding SelectedPart}" Margin="5" Grid.Column="0"> 

我的视图模型是这样的:

ViewModelClass

  • SelectedPart

名称和其他属性

  • StatusList

名称和其他属性

真的没什么复杂的,我认为......网格被绑定到所选择的客户,这就是问题所在。

2

在您的绑定中,尝试将AncestorType设置为您的视图类。喜欢的东西

ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type vw:MyView}}, Path=DataContext.PartGruppAll}" 

其中VW为您的命名空间,你让你查看和MyView的是您的视图类本身的名称。这样

xmlns:vw="clr-namespace:MyApp.View" 

在我的应用程序已经宣布大众汽车(你可能并不需要该位,但我包括以防万一=)