Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ContentPersenter中绑定Datagrid的ItemsSource_C#_Wpf_User Controls - Fatal编程技术网

C# 在ContentPersenter中绑定Datagrid的ItemsSource

C# 在ContentPersenter中绑定Datagrid的ItemsSource,c#,wpf,user-controls,C#,Wpf,User Controls,我正在创建一个包含ContentPresenter的UserControl。我在窗口中的contentPresenter中填充了一个Datagrid,将itemsSource绑定到一个列表,但它不起作用 我的数据网格是空的。但是当我把数据网格移出我的用户控制时,我得到了里面的数据 我的用户控制 XAML: 我的窗户 C: 谢谢您的绑定指向了一个窗口元素的属性,该属性不知道它是什么。它只知道窗口的属性 假设您的列表位于窗口的DataContext中。然后必须显式地将其指向窗口的DataContex

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

我的数据网格是空的。但是当我把数据网格移出我的用户控制时,我得到了里面的数据

我的用户控制 XAML:

我的窗户

C:


谢谢

您的绑定指向了一个窗口元素的属性,该属性不知道它是什么。它只知道窗口的属性

假设您的列表位于窗口的DataContext中。然后必须显式地将其指向窗口的DataContext


我用这个代码解决了我的问题

<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>
我现在不会解决这个问题,因为我想解释一下原因。 我知道它将尝试查找窗口控件的父控件的父控件,但它已在窗口中声明,因此为什么绑定没有按其名称查看windoe控件


谢谢

我尝试了你的代码,但它不起作用。我编辑我的问题以显示窗口的C代码,希望它能帮助更多。下载snoopwpf.codeplex.com。您可以使用此工具在应用程序运行时检查UI并发现绑定错误。我在一台计算机上工作,不是管理员,无法安装程序。。。
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>
public partial class MainWindow : Window
{
    public List<Object> LIST
    {
        get;
        private set;
    }

    public MainWindow()
    {
        fillList();
        InitializeComponent();
    }
}
<DataGrid ItemsSource="{Binding Path=DataContext.LIST, ElementName=win}" AutoGenerateColumns="True" />
<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>