2013-08-19 99 views
1

我有一個父DataGrid,並在那個DataGrid中我有一個內部DataGrid.My的目標是填充內部DataGrid在行上單擊我的父DataGrid.Below是我的代碼:從兩個不同的ObservableCollection綁定項目到一個控件

<DataGrid ItemsSource="{Binding GuestHostTotalList}" HorizontalAlignment="Stretch" Margin="16,15,-14,-15" x:Name="dataGridParent" 
         Style="{StaticResource EfesDataGridStyle}" FrozenColumnCount="14" GridLinesVisibility="All" 
         HorizontalGridLinesBrush="#DADADA" VerticalGridLinesBrush="#DADADA" Cursor="Hand" AutoGenerateColumns="False" 
         RowDetailsVisibilityMode="Visible" > 
       <i:Interaction.Triggers> 
        <i:EventTrigger EventName="MouseLeftButtonUp"> 
         <ei:CallMethodAction MethodName="UpdateEvent" TargetObject="{Binding}"></ei:CallMethodAction> 
        </i:EventTrigger> 
       </i:Interaction.Triggers> 
       <DataGrid.ColumnHeaderStyle> 
        <Style TargetType="DataGridColumnHeader"> 
         <Setter Property="HorizontalAlignment" Value="Stretch"/> 
         <Setter Property="Foreground" Value="White"></Setter> 
         <Setter Property="Background" Value="Black"></Setter> 
         <Setter Property="MinHeight" Value="28"></Setter> 
         <Setter Property="Cursor" Value="Hand"></Setter> 
         <Setter Property="Template"> 
          <Setter.Value> 
           <ControlTemplate TargetType="{x:Type DataGridColumnHeader}"> 
            <Grid> 
             <Grid.ColumnDefinitions> 
              <ColumnDefinition Width="*" /> 
              <ColumnDefinition Width="Auto" /> 
             </Grid.ColumnDefinitions> 
             <Border x:Name="BackgroundBorder" BorderThickness="0" Background="{StaticResource Siyah}" BorderBrush="{StaticResource Siyah}" Grid.ColumnSpan="2" /> 
             <ContentPresenter Margin="6,3,6,3" VerticalAlignment="Center" /> 
             <Path x:Name="SortArrow" Visibility="Collapsed" Data="M0,0 L1,0 0.5,1 z" Stretch="Fill" Grid.Column="3" Width="8" Height="6" Fill="White" Margin="0,0,8,0" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.4" /> 
             <Rectangle Width="1" Fill="#AAC377" HorizontalAlignment="Right" Grid.ColumnSpan="2" /> 
             <Rectangle Width="1" Margin="0,0,1,0" Fill="#425B10" HorizontalAlignment="Right" Grid.ColumnSpan="2" /> 
             <Thumb x:Name="PART_LeftHeaderGripper" Visibility="Hidden" HorizontalAlignment="Left" /> 
             <Thumb x:Name="PART_RightHeaderGripper" Visibility="Hidden" HorizontalAlignment="Right" /> 
            </Grid> 
            <ControlTemplate.Triggers> 
             <Trigger Property="SortDirection" Value="{x:Null}"> 
              <Setter TargetName="BackgroundBorder" Property="Background" Value="{DynamicResource Siyah}" /> 
              <Setter TargetName="BackgroundBorder" Property="BorderBrush" Value="Transparent" /> 
             </Trigger> 
             <MultiTrigger> 
              <MultiTrigger.Conditions> 
               <Condition Property="IsMouseOver" Value="True" /> 
               <Condition Property="SortDirection" Value="{x:Null}" /> 
              </MultiTrigger.Conditions> 
              <Setter Property="Background" TargetName="BackgroundBorder" Value="{StaticResource ASiyah}" /> 
              <Setter Property="BorderBrush" TargetName="BackgroundBorder" Value="{StaticResource ASiyah}" /> 
             </MultiTrigger> 
             <MultiTrigger> 
              <MultiTrigger.Conditions> 
               <Condition Property="IsMouseOver" Value="true" /> 
               <Condition Property="SortDirection" Value="{x:Null}" /> 
              </MultiTrigger.Conditions> 
              <Setter TargetName="BackgroundBorder" Property="Background" Value="{StaticResource ASiyah}" /> 
              <Setter TargetName="BackgroundBorder" Property="BorderBrush" Value="{StaticResource ASiyah}" /> 
             </MultiTrigger> 
             <Trigger Property="SortDirection" Value="Ascending"> 
              <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" /> 
              <Setter TargetName="SortArrow" Property="RenderTransform"> 
               <Setter.Value> 
                <RotateTransform Angle="180" /> 
               </Setter.Value> 
              </Setter> 
             </Trigger> 
             <Trigger Property="SortDirection" Value="Descending"> 
              <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" /> 
             </Trigger> 
             <Trigger Property="DisplayIndex" Value="0"> 
              <Setter Property="Visibility" Value="Collapsed" TargetName="PART_LeftHeaderGripper"></Setter> 
             </Trigger> 
            </ControlTemplate.Triggers> 
           </ControlTemplate> 
          </Setter.Value> 
         </Setter> 
         <Style.Triggers> 
          <Trigger Property="IsMouseOver" Value="True"> 
           <Setter Property="Background" Value="LightGray" /> 
          </Trigger> 
         </Style.Triggers> 
        </Style> 
       </DataGrid.ColumnHeaderStyle> 
       <DataGrid.Columns>      
        <DataGridTextColumn Header="Kod" Binding="{Binding Code}" Width="50*" /> 
        <DataGridTextColumn Header="Tarih" Binding="{Binding History}" Width="70*"/> 
        <DataGridTextColumn Header="Ev Sahibi" Binding="{Binding Own}" Width="100*"/> 
        <DataGridTextColumn Header="Konuk" Binding="{Binding Guest}" Width="50*"/> 
        <DataGridTextColumn Header="1" Binding="{Binding One_guest}" Width="30*" /> 
        <DataGridTextColumn Header="X" Binding="{Binding X_game}" Width="30*" /> 
        <DataGridTextColumn Header="2" Binding="{Binding Two_guest}" Width="30*"/> 
        <DataGridTextColumn Header="T1" Binding="{Binding T1_host}" Width="30*" /> 
        <DataGridTextColumn Header="TX" Binding="{Binding TX_host}" Width="30*" /> 
        <DataGridTextColumn Header="T2" Binding="{Binding T2_host}" Width="30*" /> 
        <DataGridTextColumn Header="+" CellStyle ="{StaticResource DataGridCellStyle}" Binding="{Binding TotalOptions}" Width="30*"/> 
        <DataGridTextColumn Header="Min" Binding="{Binding MinOption}" Width="30*" /> 
        <DataGridTextColumn Header="Lig" Binding="{Binding League}" Width="70*" /> 
        <DataGridTemplateColumn Header="Durum" Width="Auto" > 
         <DataGridTemplateColumn.CellTemplate> 
          <DataTemplate> 
           <ToggleButton x:Name="toggleBtn" VerticalAlignment="Center" Margin="0" Style="{StaticResource SlidingToggleButton}" BorderBrush="{x:Null}" IsChecked="{Binding ToggleBtn}" Background="#FF2F63FF" /> 
          </DataTemplate> 
         </DataGridTemplateColumn.CellTemplate> 
        </DataGridTemplateColumn>      
       </DataGrid.Columns> 
       <DataGrid.RowStyle> 
        <Style TargetType="{x:Type DataGridRow}"> 
         <EventSetter Event="MouseLeftButtonUp" Handler="RowClick"/> 
        </Style> 
       </DataGrid.RowStyle> 
       <DataGrid.Resources> 
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/> 
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/> 
       </DataGrid.Resources>    

       <DataGrid.RowDetailsTemplate x:Uid="test123"> 
        <DataTemplate x:Name="testTemplete"> 
         <DataGrid x:Name="innerGrid" ItemsSource="{Binding SubGridItemList}" Style="{StaticResource EfesDataGridStyle}" Margin="50,2,2,2" MinHeight="40" GridLinesVisibility="All" HorizontalGridLinesBrush="#DADADA" VerticalGridLinesBrush="#DADADA" Visibility="Collapsed" > 
          <DataGrid.ColumnHeaderStyle> 
           <Style TargetType="DataGridColumnHeader"> 
            <Setter Property="Foreground" Value="White"></Setter> 
            <Setter Property="Background" Value="Black"></Setter> 
            <Setter Property="MinHeight" Value="28"></Setter> 
            <Setter Property="Cursor" Value="Hand"></Setter> 
            <Setter Property="Template"> 
             <Setter.Value> 
              <ControlTemplate TargetType="{x:Type DataGridColumnHeader}"> 
               <Grid> 
                <Grid.ColumnDefinitions> 
                 <ColumnDefinition Width="*" /> 
                 <ColumnDefinition Width="Auto" /> 
                </Grid.ColumnDefinitions> 
                <Border x:Name="BackgroundBorder" BorderThickness="0" Background="{StaticResource Siyah}" BorderBrush="{StaticResource Siyah}" Grid.ColumnSpan="2" /> 
                <ContentPresenter Margin="6,3,6,3" VerticalAlignment="Center" /> 
                <Path x:Name="SortArrow" Visibility="Collapsed" Data="M0,0 L1,0 0.5,1 z" Stretch="Fill" Grid.Column="3" Width="8" Height="6" Fill="White" Margin="0,0,8,0" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.4" /> 
                <Rectangle Width="1" Fill="#AAC377" HorizontalAlignment="Right" Grid.ColumnSpan="2" /> 
                <Rectangle Width="1" Margin="0,0,1,0" Fill="#425B10" HorizontalAlignment="Right" Grid.ColumnSpan="2" /> 
                <Thumb x:Name="PART_LeftHeaderGripper" Visibility="Hidden" HorizontalAlignment="Left" /> 
                <Thumb x:Name="PART_RightHeaderGripper" Visibility="Hidden" HorizontalAlignment="Right" /> 
               </Grid> 
               <ControlTemplate.Triggers> 
                <Trigger Property="SortDirection" Value="{x:Null}"> 
                 <Setter TargetName="BackgroundBorder" Property="Background" Value="{DynamicResource Siyah}" /> 
                 <Setter TargetName="BackgroundBorder" Property="BorderBrush" Value="Transparent" /> 
                </Trigger> 
                <MultiTrigger> 
                 <MultiTrigger.Conditions> 
                  <Condition Property="IsMouseOver" Value="True" /> 
                  <Condition Property="SortDirection" Value="{x:Null}" /> 
                 </MultiTrigger.Conditions> 
                 <Setter Property="Background" TargetName="BackgroundBorder" Value="{StaticResource ASiyah}" /> 
                 <Setter Property="BorderBrush" TargetName="BackgroundBorder" Value="{StaticResource ASiyah}" /> 
                </MultiTrigger> 
                <MultiTrigger> 
                 <MultiTrigger.Conditions> 
                  <Condition Property="IsMouseOver" Value="true" /> 
                  <Condition Property="SortDirection" Value="{x:Null}" /> 
                 </MultiTrigger.Conditions> 
                 <Setter TargetName="BackgroundBorder" Property="Background" Value="{StaticResource ASiyah}" /> 
                 <Setter TargetName="BackgroundBorder" Property="BorderBrush" Value="{StaticResource ASiyah}" /> 
                </MultiTrigger> 
                <Trigger Property="SortDirection" Value="Ascending"> 
                 <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" /> 
                 <Setter TargetName="SortArrow" Property="RenderTransform"> 
                  <Setter.Value> 
                   <RotateTransform Angle="180" /> 
                  </Setter.Value> 
                 </Setter> 
                </Trigger> 
                <Trigger Property="SortDirection" Value="Descending"> 
                 <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" /> 
                </Trigger> 
                <Trigger Property="DisplayIndex" Value="0"> 
                 <Setter Property="Visibility" Value="Collapsed" TargetName="PART_LeftHeaderGripper"></Setter> 
                </Trigger> 
               </ControlTemplate.Triggers> 
              </ControlTemplate> 
             </Setter.Value> 
            </Setter> 
            <Style.Triggers> 
             <Trigger Property="IsMouseOver" Value="True"> 
              <Setter Property="Background" Value="LightGray" /> 
             </Trigger> 
            </Style.Triggers> 
           </Style> 
          </DataGrid.ColumnHeaderStyle> 
          <DataGrid.RowStyle> 
           <Style TargetType="{x:Type DataGridRow}"> 
            <EventSetter Event="MouseLeftButtonUp" Handler="InnerGridRowClick"/> 
           </Style> 
          </DataGrid.RowStyle> 
          <DataGrid.Columns> 
           <DataGridTextColumn Header="Kod" Binding="{Binding Kod}" Width="400" /> 
           <DataGridTextColumn Header="Alt Secenakler" Binding="{Binding Alt_Secenakler}" Width="500" />         
          </DataGrid.Columns> 
          <DataGrid.Resources> 
           <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/> 
           <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/> 
          </DataGrid.Resources> 
         </DataGrid>      
        </DataTemplate> 
       </DataGrid.RowDetailsTemplate> 
      </DataGrid> 

在上面的代碼,你可以看到dataGridParent是父DataGrid和innerGrid是內DataGrid.Now我在我的視圖模型1.GuestHostTotalList兩個可觀察集合(是我的父母DataGrid的的ItemSource)和2.SubGridItemList(此被用作我的innerDataGrid的ItemSource)。但我看到內部網格沒有填充數據。 任何人都可以幫助我。提前感謝。

回答

0

您已在DataGrid.RowDetailsTemplate內定義了'內部'DataGrid。此Template用於顯示行詳細信息。因此,您的SubGridItemList集合應該位於「外部」DataGrid的每一行中顯示的數據對象中。

因此,您有一個名爲GuestHostTotalList的集合,它綁定到外部DataGrid.ItemsSource屬性。無論本集合中的數據類型是什麼類型的數據都需要添加集合屬性以綁定到內部DataGrid.ItemSource如果您希望綁定起作用。

+0

因此,根據你沒有辦法我可以綁定兩個不同的可觀察集合,這些集合與我的innerDataGrid.thanks無關,用於回覆。 –

+0

這不是我說的。你不清楚你實際想要達到什麼......你說你有一個錯誤,所以我解釋了你爲什麼會出錯。 – Sheridan

+0

好的。那麼@Sheridan在現在的情況下可能知道什麼是解決這個問題的最好的方法,或者什麼是最好的方法來做到這一點。我的目標是點擊一行數據必須填充到我的內部網格中。現在這些數據可能與行數據有關,也可能不是。所以我創建了這個單獨的observableCollection.Now我想知道什麼是最好的方法來實現這一點。請給我建議。感謝您。 –

0

您不能直接將內部DataGrid綁定到集合「SubGridItemList」,因爲現在它的DataContext是「GuestHostTotalList」的DataContext。你必須做的是能夠從「GuestHostTotalList」跳轉到ViewModel DataContext。

請找我解決了類似的問題在這裏: EventTrigger not working inside ItemsControl in MVVM

通過使用您可以從「GuestHostTotalList」範圍內跳轉到視圖模型背景下,將能夠綁定「SubGridItemList」的DataContextProxy類:{綁定DataSource.SubGridItemList,源= {StaticResource DataContextProxy}

相關問題