将项目从两个不同的ObservableCollection绑定到一个控件

时间:2013-08-19 13:31:25

标签: wpf silverlight xaml mvvm mvvm-light

我有一个父DataGrid,在DataGrid里面我有一个内部DataGrid.My目标是在我的父DataGrid的行点击上填充内部数据网格.Below是我的代码:

<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是父数据网格,而innerGrid是内部DataGrid。我在我的viewmodel中有两个可观察的集合1.GuestHostTotalList(是我的父DataGrid的itemsource)和2.SubGridItemList(这是用的)作为我的innerDataGrid的ItemSource)。但我看到内部网格没有填充数据。 任何人都可以帮助我。谢谢。

2 个答案:

答案 0 :(得分:0)

您已在DataGrid内定义了“内部”DataGrid.RowDetailsTemplate。此Template用于显示行详细信息。因此,您的SubGridItemList集合应该位于“外部”DataGrid的每一行中显示的数据对象中。

因此,您有一个名为GuestHostTotalList的集合,它绑定到外部DataGrid.ItemsSource属性。无论此集合中的数据类型是什么,如果您希望绑定工作,需要添加集合属性以绑定到内部DataGrid.ItemSource的数据类型。

答案 1 :(得分:0)

您无法直接将内部DataGrid绑定到集合“SubGridItemList”,因为现在它的DataContext是“GuestHostTotalList”的DataContext。您需要做的是能够从“GuestHostTotalList”跳转到ViewModel DataContext。

请在此处找到类似问题的解决方案: EventTrigger not working inside ItemsControl in MVVM

通过使用DataContextProxy类,您可以从“GuestHostTotalList”上下文跳转到ViewModel上下文,并且能够绑定“SubGridItemList”:{Binding DataSource.SubGridItemList,Source = {StaticResource DataContextProxy}