2017-09-22 7 views
0

CompositeCollectionを使用して、1つのitemsControl内に異なるコレクションを表示しようとしています。私はさまざまなクラスの種類のために複数のDataTemplatesを作成しました。ただし、プログラムはデータテンプレートではなくclass.ToString()を表示します。 this answerによれば、タイプ{x:Type}が指定されていますが、動作しません。私は何かを見逃しましたか?ここでCompositeCollectionのDataTemplateが機能しない

はXAMLです:ここでは

<Window x:Class="TestCompositeConnection.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:TestCompositeConnection" 
     mc:Ignorable="d" 
     Title="MainWindow" 
     Height="350" 
     Width="525"> 
    <Grid> 
     <ListBox Name="myListBox" 
       Height="300" 
       Width="200" 
       Background="White"> 
      <ListBox.Resources> 
       <DataTemplate DataType="x:Type local:MyRectangle"> 
        <WrapPanel> 
         <TextBlock Text="{Binding Width}"></TextBlock> 
         <TextBlock Text="{Binding Height}"></TextBlock> 
        </WrapPanel> 
       </DataTemplate> 
       <DataTemplate DataType="x:Type local:MyLine"> 
        <StackPanel> 
         <TextBox Text="{Binding EndX}"></TextBox> 
         <TextBox Text="{Binding EndY}"></TextBox> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.Resources> 
     </ListBox> 
    </Grid> 
</Window> 

は、コードが背後にある:

public partial class MainWindow : Window 
    { 
     public CompositeCollection Data { get; set; } 

     public ObservableCollection<MyRectangle> Rects { get; set; } 
     public ObservableCollection<MyLine> Lines { get; set; } 

     public MainWindow() 
     { 
      InitializeComponent(); 

      Data = new CompositeCollection(); 

      Rects = new ObservableCollection<MyRectangle>(); 
      Lines = new ObservableCollection<MyLine>(); 

      Rects.Add(new MyRectangle 
      { 
       X = 100, 
       Y = 100, 
       Width = 100, 
       Height = 100 
      }); 

      Lines.Add(new MyLine 
      { 
       StartX = 200, 
       StartY = 3, 
       EndX = 300, 
       EndY = 100 
      }); 

      Data.Add(new CollectionContainer() { Collection = Rects }); 
      Data.Add(new CollectionContainer() { Collection = Lines }); 

      myListBox.ItemsSource = Data; 
     } 
    } 

答えて

0

x:Type属性値の周りに中括弧を入れてみてください:

<DataTemplate DataType="{x:Type local:MyRectangle}"> 
    <WrapPanel> 
     <TextBlock Text="{Binding Width}"></TextBlock> 
     <TextBlock Text="{Binding Height}"></TextBlock> 
    </WrapPanel> 
</DataTemplate> 
<DataTemplate DataType="{x:Type local:MyLine}"> 
    <StackPanel> 
     <TextBox Text="{Binding EndX}"></TextBox> 
     <TextBox Text="{Binding EndY}"></TextBox> 
    </StackPanel> 
</DataTemplate> 
+0

はい、私はちょうど見つけこの愚かな間違いを自分で...ありがとう...私はすでにこれに時間を費やしている... – Siamca

関連する問題