【发布时间】:2013-10-25 01:23:51
【问题描述】:
我正在为我们的 WPF 应用程序构建一个 UI 元素,它允许用户可视化以网格格式对齐的图形集合。据我了解,您可以将 ItemsControl 与 WrapPanel 一起使用,它可以很好地以网格格式对齐 ui 元素。
当我们尝试使用 winforms 图形库 (zedgraph) 生成图形时,困难就来了。这意味着我们必须使用WindowsFormsHost 来显示图形视图。我尝试使用普通的数据绑定来绑定图形集合,它并没有真正起作用:
XAML:
<ItemsControl ItemsSource="{Binding Graphs}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<WindowsFormsHost Margin="5">
<ui:Graph></ui:Graph>
</WindowsFormsHost>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
上面的代码没有显示任何内容,但是访问了 Graphs getter。
另外,即使我取消绑定,我只是在 ItemsControl 中插入一些随机图形视图...它仍然不显示任何内容:
XAML:
<ItemsControl>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<WindowsFormsHost Margin="5">
<ui:Graph></ui:Graph>
</WindowsFormsHost>
<WindowsFormsHost Margin="5">
</WindowsFormsHost>
<ui:Graph></ui:Graph>
</WindowsFormsHost>
<WindowsFormsHost Margin="5">
<ui:Graph></ui:Graph>
</WindowsFormsHost>
</ItemsControl>
为了测试以确保图形库正常运行,这确实显示了一个图形:
<Grid>
<WindowsFormsHost Margin="5">
<ui:Graph></ui:Graph>
</WindowsFormsHost>
</Grid>
谁能指引我正确的方向?非常感谢。
【问题讨论】:
-
我强烈建议创建一个
UserControl来托管您的 winforms 内容并在此 UserControl 中创建 DependencyProperties,以便您可以获得绑定功能。 -
感谢您的建议。知道为什么不显示非绑定版本吗?图表在
ItemsControl之外很好地显示,但在ItemsControl内部却没有。 -
我猜这可能与布局有关。 winforms 很可怕,并且要求您对所有内容的大小进行硬编码。尝试将固定的硬编码
Width和Height提供给 winformshost。 -
做到了...为图表设置硬编码大小有效。事实上,绑定也有效。你应该把它作为一个答案。谢谢!
标签: c# wpf winforms xaml data-binding