我有一个带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
发布评论