[英]WPF Canvas items and DataTemplate
Is there a way that I can use non-UIElements in a canvas if I have a DataTemplate (or something similar) for them? 如果我有一个DataTemplate(或类似的东西),我有没有办法在画布中使用非UIElements? I feel like I have done this before, and that it is possible, but I can't figure it out.
我觉得我以前做过这件事,而且有可能,但我无法弄清楚。 Here is some code...
这是一些代码......
<Window x:Class="EntityTranslator.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:EntityTranslator"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:Entity x:Key="DesignEntity}" EntityName="Test" />
<DataTemplate DataType="{x:Type local:Entity}">
<StackPanel>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<Canvas>
<local:Entity EntityName="Test" />
</Canvas>
</Grid>
</Window>
Wrap them in a ContentPresenter or ContentControl , which are controls that can host any object type in their Content
将它们包装在ContentPresenter或ContentControl中 ,这些控件可以在其
Content
托管任何对象类型
<ContentPresenter>
<local:Entity EntityName="Test" />
</ContentPresenter>
The ContentPresenter
will draw the item using your implicit DataTemplate
automatically ContentPresenter
将自动使用隐式DataTemplate
绘制项目
Your problem here is that you cant add model items to a Panel, just UI elements. 这里的问题是你不能将模型项添加到Panel,只是UI元素。 For to do this that you want, you need to do some like this:
要做到你想要的,你需要这样做:
<Window.Resources>
<DataTemplate DataType="{x:Type WpfApplication2:Entity}">
<StackPanel>
<TextBlock Text="{Binding EntityName}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
Your resources, and: 您的资源,以及:
<ListBox ItemsSource={Binding SomeEntityCollection}>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
Try this, and also you may set the X and Y properties from the model, setting the ItemsContainerStyle. 试试这个,你也可以从模型中设置X和Y属性,设置ItemsContainerStyle。 Hope this works for you...
希望这对你有用......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.