DataGrid嵌套在DataGrid问题

时间:2017-01-05 20:02:44

标签: c# wpf xaml datagrid

我正在使用"当使用ItemsSource时,操作无效。使用ItemsControl.ItemsSource访问和修改元素"错误,我不知道为什么。

我正在尝试将数据网格嵌套在数据网格中。如果我只使用文本块它没有问题,但是一旦我分配了值并单击其中一个父数据网格元素,我就会得到上述错误。

这是我的xaml:

    <DataGrid Grid.Row="1" Name="dgSREducation" Background="White" AutoGenerateColumns="False" CanUserAddRows="False" HorizontalAlignment="Left">
     <DataGrid.Columns>
        <DataGridTextColumn Header="Description" Binding="{Binding Description}"/>
        <DataGridTextColumn Header="Link" Binding="{Binding Link}"/>
        <DataGridTextColumn Header="Type" IsReadOnly="True" Binding="{Binding Type}"/>
        <DataGridCheckBoxColumn Header="Active" Binding="{Binding Active}"/>
     </DataGrid.Columns>
     <DataGrid.RowDetailsTemplate>
        <DataTemplate>
           <StackPanel>
              <DataGrid ItemsSource="{Binding Conditions}" AutoGenerateColumns="False" CanUserAddRows="False">
                 <DataGridTextColumn Header="Notification Type" IsReadOnly="True" Binding="{Binding Notification}"/>
                 <DataGridTextColumn Header="Value" Binding="{Binding Value}"/>
                 <DataGridTextColumn Header="Compare Type" IsReadOnly="True" Binding="{Binding Compare}"/>
                 <DataGridCheckBoxColumn Header="Active" Binding="{Binding Active}"/>
              </DataGrid>
              <TextBlock Text="{Binding Words}"></TextBlock>
           </StackPanel>
        </DataTemplate>
     </DataGrid.RowDetailsTemplate>
     <DataGrid.Resources>
        <Style TargetType="DataGridRow">
           <EventSetter Event="MouseLeftButtonDown" Handler="Education_Click"/>
        </Style>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.HighlightColor}"></SolidColorBrush>
     </DataGrid.Resources>
  </DataGrid>

我所有的课程是:

class SREducationData
{
   public NamedIDData Type { get; set; }
   public bool Active { get; set; }
   public string Link { get; set; }
   public string Description { get; set; }
   public long ID { get; set; }
   public ObservableCollection<ConditionData> Conditions { get; set; }

   public string Words { get; set; }
}

class ConditionData
{
   public long ID { get; set; }
   public NamedIDData Notification { get; set; }
   public bool Active { get; set; }
   public string Value { get; set; }
   public NamedIDData Compare { get; set; }
}

当我的程序最初加载时,我在代码中设置ItemSource,方法是dgSREducation.ItemsSource = display;,其中显示为ObservableCollection<SREducationData> display;

当我点击其中一个dgSREducation的元素时,我收到错误,并在该元素下面出现一个空白的数据网格。有关正在发生的事情的任何想法?

1 个答案:

答案 0 :(得分:3)

糟糕,您没有将嵌套的datagrid列放在&lt; DataGrid.Columns&gt;中。

变化:

<DataGrid ItemsSource="{Binding Conditions}" AutoGenerateColumns="False" CanUserAddRows="False">
    <DataGridTextColumn Header="Notification Type" IsReadOnly="True" Binding="{Binding Notification}"/>
    <DataGridTextColumn Header="Value" Binding="{Binding Value}"/>
    <DataGridTextColumn Header="Compare Type" IsReadOnly="True" Binding="{Binding Compare}"/>
    <DataGridCheckBoxColumn Header="Active" Binding="{Binding Active}"/>
</DataGrid>

要:

<DataGrid ItemsSource="{Binding Conditions}" AutoGenerateColumns="False" CanUserAddRows="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Notification Type" IsReadOnly="True" Binding="{Binding Notification}"/>
        <DataGridTextColumn Header="Value" Binding="{Binding Value}"/>
        <DataGridTextColumn Header="Compare Type" IsReadOnly="True" Binding="{Binding Compare}"/>
        <DataGridCheckBoxColumn Header="Active" Binding="{Binding Active}"/>
    </DataGrid.Columns>
</DataGrid>