[英]WPF Canvas items and DataTemplate
如果我有一個DataTemplate(或類似的東西),我有沒有辦法在畫布中使用非UIElements? 我覺得我以前做過這件事,而且有可能,但我無法弄清楚。 這是一些代碼......
<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>
將它們包裝在ContentPresenter或ContentControl中 ,這些控件可以在其Content
托管任何對象類型
<ContentPresenter>
<local:Entity EntityName="Test" />
</ContentPresenter>
ContentPresenter
將自動使用隱式DataTemplate
繪制項目
這里的問題是你不能將模型項添加到Panel,只是UI元素。 要做到你想要的,你需要這樣做:
<Window.Resources>
<DataTemplate DataType="{x:Type WpfApplication2:Entity}">
<StackPanel>
<TextBlock Text="{Binding EntityName}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
您的資源,以及:
<ListBox ItemsSource={Binding SomeEntityCollection}>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
試試這個,你也可以從模型中設置X和Y屬性,設置ItemsContainerStyle。 希望這對你有用......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.