2

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>
4

2 に答える 2

4

それらをContentPresenterまたはContentControlでラップします。これらは、任意のオブジェクト タイプをホストできるコントロールです。Content

<ContentPresenter>
    <local:Entity EntityName="Test" />
</ContentPresenter>

は、暗黙的に自動的ContentPresenterにアイテムを描画しますDataTemplate

于 2012-10-12T19:19:54.950 に答える
2

ここでの問題は、モデル項目を 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 を設定することもできます。これがうまくいくことを願っています...

于 2012-10-12T19:28:04.453 に答える