用户控件中DataConrid的DataContext(DataContext to DataGrid in user control)

编程入门 行业动态 更新时间:2024-10-23 23:22:08
用户控件中DataConrid的DataContext(DataContext to DataGrid in user control)

我有一个带tabcontrol的主窗口。 添加带有用户控件内容的新tabitem。 在xaml中:

<Grid><DataGrid DataContext="{Binding Path=Patients, Mode=TwoWay}"> <DataGrid.Columns> <DataGridTextColumn Header="Id" Width="Auto" Binding="{Binding Id}"/>

在代码背后:打开上下文,

var query = from pp in context.Patients select pp; var Patients = query.ToList(); TabItem patientsView = new TabItem(); // adding new tabitem StackPanel header = new StackPanel header.Children.Add(new TextBlock {Text="Patients"}); patientsView.Header = header; patientsView.Content = new ViewDataPatients{DataContext = Patients};

它拒绝将绑定数据填充到网格中。 知道我在做错了吗?

I have a main window with tabcontrol. Adding a new tabitem with a user control content. In the xaml:

<Grid><DataGrid DataContext="{Binding Path=Patients, Mode=TwoWay}"> <DataGrid.Columns> <DataGridTextColumn Header="Id" Width="Auto" Binding="{Binding Id}"/>

In code behind: Opening context,

var query = from pp in context.Patients select pp; var Patients = query.ToList(); TabItem patientsView = new TabItem(); // adding new tabitem StackPanel header = new StackPanel header.Children.Add(new TextBlock {Text="Patients"}); patientsView.Header = header; patientsView.Content = new ViewDataPatients{DataContext = Patients};

It refuses to populate the bind data to the grid. Any idea where I am doing this wrong?

最满意答案

无法理解为什么你需要这样的方法,但仍然。 您已将UserControl的DataContext设置为Patiense,因此DataGrid同时具有此数据上下文,请执行以下操作:

<DataGrid DataContext="{Binding}">

但在你的情况下,这是多余的。 要使用刚刚设置的ItemsSource数据填充DataGrid:

<DataGrid ItemsSource="{Binding}">

希望这可以帮助。

金问候,纳扎尔

can't understand why you need such approach, but still. You've set DataContext of your UserControl to Patiense, so DataGrid alreay have this data context, enought the following:

<DataGrid DataContext="{Binding}">

But this is redundant in your case. To populate DataGrid with data just set ItemsSource:

<DataGrid ItemsSource="{Binding}">

Hope this helps.

King regards, Nazar

更多推荐

Patients,new,DataContext,tabitem,电脑培训,计算机培训,IT培训"/> <meta name=&q

本文发布于:2023-08-02 18:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1380680.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   用户   DataConrid   DataContext   user

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!