2016-11-05 6 views
0

ネストされたクラスコレクションを持つクラスを管理するためにネストされたDataGridを使用しています。ネストされたDataGridで「アイテムコレクションは必須です」例外が発生するのはなぜですか?

それぞれのネストされたクラスは、セルテンプレートで表されます。

これは、問題を再現する最小限のモックアップです:

using System.ComponentModel; 
using System.Linq; 

namespace ICMBE{ 
    public class Outer : INotifyPropertyChanged { 
     public event PropertyChangedEventHandler PropertyChanged; 

     private Nested1[ ] _categories = Enumerable.Range(1, 1 
        ).Select(n => new Nested1()).ToArray(); 

     public Nested1[ ] Categories { 
      get { return this._categories; } 
     } 
    } 

    public class Nested1 : INotifyPropertyChanged { 
     public event PropertyChangedEventHandler PropertyChanged; 

     private Nested2[ ] _questions = Enumerable.Range(1, 5 
        ).Select(n => new Nested2()).ToArray(); 

     public Nested2[ ] Questions { 
      get { return this._questions; } 
     } 
    } 

    public class Nested2 : INotifyPropertyChanged { 
     public event PropertyChangedEventHandler PropertyChanged; 

     private Nested3[] _answers = Enumerable.Range(1, 5 
       ).Select(n => new Nested3()).ToArray(); 

     public Nested3[ ] Answers { 
      get { return this._answers; } 
     } 
    } 

    public class Nested3 : INotifyPropertyChanged { 
     public event PropertyChangedEventHandler PropertyChanged; 

     private string _foo = "Bar"; 

     public string Foo { get { return this._foo; } } 
    } 
} 

は、フィールドの値を作成し、アプリケーションでそれを公開:

using System.ComponentModel; 

namespace IMCBE{ 
    public partial class App : INotifyPropertyChanged { 
     public event PropertyChangedEventHandler PropertyChanged; 
     private Outer _outer = new Outer(); 
    } 
} 

namespace IMCBE{ 
public partial class Program { 
     /// <summary> 
     /// Bla bla bla 
     /// </summary> 
     public Outer Outer{ 
      get { return this._outer; } 
     } 
    } 
} 

は、このウィンドウを使用して -

<Window 
    x:Class="ICMBE.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:Controls="clr-namespace:WPFTools.Controls;assembly=WPFTools" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:WhyAreMyRowNumbersChanging" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <!-- Answers --> 
     <DataTemplate x:Key="dtAnswerTemplate"> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="100" /> 
        <ColumnDefinition Width="5" /> 
        <ColumnDefinition /> 
       </Grid.ColumnDefinitions> 
       <ToggleButton> 
        <Viewbox> 
         <TextBlock Text="Correct" /> 
        </Viewbox> 
       </ToggleButton> 
       <TextBox Text="Bar" /> 
      </Grid> 
     </DataTemplate> 

     <!-- Questions --> 
     <DataTemplate x:Key="dtQuestionTemplate"> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="25" /> 
        <RowDefinition Height="5" /> 
        <RowDefinition Height="25" /> 
        <RowDefinition Height="5" /> 
        <RowDefinition Height="50" /> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="128" /> 
        <ColumnDefinition Width="5" /> 
        <ColumnDefinition Width="128" /> 
        <ColumnDefinition Width="5" /> 
        <ColumnDefinition /> 
       </Grid.ColumnDefinitions> 
       <Viewbox Grid.ColumnSpan="3"> 
        <TextBlock Text="Question" /> 
       </Viewbox> 
       <TextBox Grid.Row="2" Grid.ColumnSpan="3" Text="Foo" /> 
       <ToggleButton Grid.Row="4"> 
        <Viewbox> 
         <TextBlock Text="Bonus" /> 
        </Viewbox> 
       </ToggleButton> 
       <Button Grid.Row="4" Grid.Column="2" /> 
       <Viewbox Grid.Column="4"> 
        <TextBlock Text="Answers" /> 
       </Viewbox> 
       <DataGrid 
        AutoGenerateColumns="False" CanUserAddRows="False" 
        CanUserDeleteRows="False" Grid.Column="4" 
        Grid.RowSpan="4" ItemsSource="{Binding Nested}"> 
        <DataGridTemplateColumn 
         Width="*" 
         CellTemplate="{StaticResource dtAnswerTemplate}" /> 
       </DataGrid> 
      </Grid> 
     </DataTemplate> 

     <!-- Categories --> 
     <DataTemplate x:Key="dtCategoryTemplate"> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="25" /> 
        <RowDefinition /> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="256" /> 
        <ColumnDefinition Width="5" /> 
        <ColumnDefinition /> 
       </Grid.ColumnDefinitions> 
       <Viewbox> 
        <TextBlock Text="Outer"/> 
       </Viewbox> 
       <DataGrid 
        AutoGenerateColumns="False" CanUserAddRows="False" 
        CanUserDeleteRows="False" Grid.Row="2" 
        Grid.ColumnSpan="3" HeadersVisibility="None" 
        ItemsSource="{Binding Nested}"> 
        <DataGrid.Columns> 
         <DataGridTemplateColumn 
          Width="*" 
          CellTemplate="{StaticResource dtQuestionTemplate}" /> 
        </DataGrid.Columns> 
       </DataGrid> 
      </Grid> 
     </DataTemplate> 
    </Window.Resources> 
    <DataGrid 
     Grid.Row="1" 
     ItemsSource="{Binding Outer.Nested, Source={x:Static Application.Current}}" 
     MaxHeight="Infinity" ScrollViewer.CanContentScroll="True"> 
     <DataGrid.Columns> 
      <DataGridTemplateColumn 
       Width="*" CellTemplate="{StaticResource dtCategoryTemplate}" /> 
     </DataGrid.Columns> 
    </DataGrid> 
</Window> 

これを実行すると、プログラムがちょうどクラッシュします。例外は「アイテムコレクションは空でなければなりません」です。

私が読んだところでは、ItemsItemsSourceの両方が設定されていると、これが起こりますが、ここでは起こりません。

このクラッシュはなぜ発生しますか?あなたがアイテムを設定するとのItemsSource の両方が設定されている場合、私は何を読んでから、

+1

@Clemens FFFFFUUUUUUUUUUUUUUU ...グッドキャッチ。それはとてもシンプルだったのでうれしい... – Will

答えて

1

が、これは起こります。あなたはdtQuestionTemplateのDataTemplateでのDataGridにDataGridTemplateColumn周り<DataGrid.Columns>タグを置くことを忘れてしまったので

これは、実際にはエラーの原因です。

それは次のようになります。

<DataGrid ... ItemsSource="{Binding Nested}"> 
    <DataGrid.Columns> 
     <DataGridTemplateColumn 
      Width="*" CellTemplate="{StaticResource dtAnswerTemplate}" /> 
    </DataGrid.Columns> 
</DataGrid> 
関連する問題