WPF 项目源绑定
我的 WPF 应用程序中有一个 Datagrid 控件,我试图将该控件绑定到主窗口类中的 ObservableCollection 属性。我尝试绑定的属性定义为:
private ObservableCollection<RequestResult> m_SentRequests = new ObservableCollection<RequestResult>();
public ObservableCollection<RequestResult> SentRequests { get { return m_SentRequests; } }
我的数据网格位于将数据上下文设置为 MainWindow 的组中:
<GroupBox Header="Results" Height="275" HorizontalAlignment="Stretch" Margin="0,305,0,0" Name="grpResults" VerticalAlignment="Top" Width="712" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:MainWindow, AncestorLevel=1}}">
<Grid>
<DataGrid AutoGenerateColumns="False" Height="246" HorizontalAlignment="Stretch" Margin="6,6,6,0" Name="dgResults" VerticalAlignment="Top" ItemsSource="{Binding Path=SentRequests}" DataContext="{Binding}" IsSynchronizedWithCurrentItem="True" />
</Grid>
</GroupBox>
我遇到的问题是在属性窗口中,在我选择 SentRequests 作为我的 ItemsSource 之后,我仍然无法选择“编辑属性绑定列”选项。我收到一个“您必须先设置 ItemsSource,然后才能执行此操作”对话框。选择“生成列”和“删除列”时出现相同的错误。就好像我没有在对话框的 ItemsSource 属性中设置任何内容。
我可以将 AutoGenerateColumns 设置为 true,但我看到我的数据已绑定(但是,不是与我想要显示的列绑定)。
我对 WPF 非常陌生,我只是将其编写为一个用于测试 Windows 服务的快速测试应用程序。
有人知道我在这里做错了什么吗?
I have a Datagrid control in my WPF application and I am trying to bind that control to an ObservableCollection property in my Main Window's class. The property I'm trying to bind to is defined as:
private ObservableCollection<RequestResult> m_SentRequests = new ObservableCollection<RequestResult>();
public ObservableCollection<RequestResult> SentRequests { get { return m_SentRequests; } }
My datagrid is in a group by which has the datacontext set to the MainWindow:
<GroupBox Header="Results" Height="275" HorizontalAlignment="Stretch" Margin="0,305,0,0" Name="grpResults" VerticalAlignment="Top" Width="712" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:MainWindow, AncestorLevel=1}}">
<Grid>
<DataGrid AutoGenerateColumns="False" Height="246" HorizontalAlignment="Stretch" Margin="6,6,6,0" Name="dgResults" VerticalAlignment="Top" ItemsSource="{Binding Path=SentRequests}" DataContext="{Binding}" IsSynchronizedWithCurrentItem="True" />
</Grid>
</GroupBox>
The problem that I'm having is that in the properties window, after I select SentRequests as my ItemsSource, I still can't select the "Edit Property-Bound Columns" option. I get a "You must set ItemsSource before you can perform this action" dialog. I get the same error when selecting "Generate Columns" and "Remove Columns". It's as if I haven't set anything in the ItemsSource property for my Dialog.
I can set AutoGenerateColumns to true though and I see my data get bound though (however, not with the columns I want to show).
I'm very new to WPF and I'm just writing this as a quick test app for testing a windows service.
Any one know what I'm doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您不需要 itemSource 中的 Path 参数。您应该能够将绑定设置为 ItemsSource={Binding SentRequests}
您还可以在代码中绑定到网格项目源,例如,如果我创建一个虚拟集合:
然后填充它,
然后我可以在主页中设置项目源构造函数或方法为:
this.gridviewname.ItemsSource = "AllPeople";
I don't believe you need the Path parameter within your itemSource. You should be able to just set the binding as ItemsSource={Binding SentRequests}
You can also bind to the grid item source in code for example if I create a dummy collection:
and then populate it
I could then set the items source in the mainpage contsructor or method as:
this.gridviewname.ItemsSource = "AllPeople";
这可能是设计者在不不断编译的情况下进行渲染的一些技巧(例如跳过代码隐藏构造函数)的结果。尝试将您的集合移至单独的类,并使用该类的实例作为您的 DataContext(如 MVVM ViewModel)。另一个类应该能够正常初始化并向设计者提供绑定属性。
This is probably a result of some of the trickery that the designer does to render without constantly compiling (like skipping code-behind constructors). Try moving your collection to a separate class and use an instance of that as your DataContext (like an MVVM ViewModel). The other class should be able to initialize normally and provide the bound property to the designer.
您是否尝试过不使用 DataContext 标签?在 GroupBox 和 DataGrid 中。
编辑
类似这样的内容:
Have you tryed without the DataContext tags? Both in GroupBox and DataGrid.
EDIT
something like this: