2014-10-16 85 views
1

我正在创建一个包含ContentPresenter的UserControl。我使用Datagrid将contentPresenter填充到窗口中,将itemsSource绑定到列表,但不起作用。在ContentPersenter中绑定ItemsSource Datagrid

我的Datagrid是空的。但是当我将DataGrid移出UserControl时,我得到了Inside数据。

我的用户 XAML:

<UserControl Name="Instance" ...> 
    <Grid> 
     <ScrollViewer> 
      <ContentPresenter Content="{Binding Path=AdditionnalContent, ElementName=Instance}" /> 
     </ScrollViewer> 
    </Grid> 
</UserControl> 

C#:

public Object AdditionnalContent 
{ 
    get { return (object)GetValue(ContentProperty); } 
    set { SetValue(ContentProperty, value); } 
} 

public static readonly DependencyProperty AdditionnalContentProperty = DependencyProperty.Register("AdditionnalContent", typeof(object), typeof(MyUserControl), 
     new PropertyMetadata(null)); 

我的窗口

<Window Name="win" ...> 
    <Grid> 
     <my:MyUserControl> 
      <my:MyUserControl.AdditionnalContent> 
       <!-- Datagrid is empty --> 
       <DataGrid ItemsSource="{Binding LIST, ElementName=win}" AutoGenerateColumns="True" /> 
      </my:MyUserControl.AdditionnalContent> 
     </my:MyUserControl> 

     <!-- Datagrid have content --> 
     <DataGrid ItemsSource="{Binding LIST, ElementName=win}" AutoGenerateColumns="True" /> 
    </Grid> 
</Window> 

C#:

public partial class MainWindow : Window 
{ 
    public List<Object> LIST 
    { 
     get; 
     private set; 
    } 

    public MainWindow() 
    { 
     fillList(); 
     InitializeComponent(); 
    } 
} 

感谢

+0

下载snoopwpf.codeplex.com。您可以使用此工具在应用程序运行时检查UI,并发现绑定错误。 – 2014-10-16 19:13:27

+0

我在电脑上工作,我不是管理员,我不能安装程序... – Northik 2014-10-16 19:36:38

回答

0

我解决我的问题与此代码

<Window Name="win" ...> 
    <Grid> 
     <my:MyUserControl> 
      <my:MyUserControl.AdditionnalContent> 
       <DataGrid ItemsSource="{Binding LIST, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" /> 
      </my:MyUserControl.AdditionnalContent> 
     </my:MyUserControl> 
    </Grid> 
</Window> 

我解决不了现在答案,因为我想了解的振振有辞的解释。 我知道它会试图找到父窗口控件的父窗口,但它已经在窗口中声明,所以绑定为什么没有看到由他的名字控制的窗口。

谢谢

0

你结合指向一个Window元素的属性,它不知道它是什么。它只知道的路径是Window的属性

假设您的LIST位于Window的DataContext之内。您必须明确指出WindowDataContext

<DataGrid ItemsSource="{Binding Path=DataContext.LIST, ElementName=win}" AutoGenerateColumns="True" /> 
+0

我尝试你的代码,但它不工作。我编辑我的问题以显示窗口的C#代码,希望它会有所帮助。 – Northik 2014-10-16 19:05:32

相关问题