WPF Datagrid无法选择行

时间:2013-04-29 06:39:35

标签: c# wpf xaml

我正在使用tabcontrol,tabcontrol中有两个选项卡,每个选项卡中有一个数据网格。但是,现在我可以在第一个数据网格中选择行,但不能在第二个数据网格中选择行。我不知道为什么我不能在第二个数据网格中选择行。

以下是我的xaml:

<TabControl Height="547" HorizontalAlignment="Left" Margin="12,14,0,0" Name="ManageTab" VerticalAlignment="Top" Width="768" Grid.RowSpan="2" 
                    SelectionChanged="ManageTab_SelectionChanged">
            <TabItem Header="STUT" Name="tab1">
                <Grid Height="520" Width="765" DataContext="{StaticResource UserViewSource}">
                    <DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="514" HorizontalAlignment="Left" 
                  ItemsSource="{Binding}" Name="UserDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected"
                  AlternatingRowBackground="LightBlue" VerticalContentAlignment="Center"
                  HorizontalGridLinesBrush="Gray" VerticalGridLinesBrush="Gray"
                  VerticalAlignment="Top" Width="346" SelectedCellsChanged="UserDataGrid_SelectedCellsChanged">
                        <DataGrid.Columns>
                            <DataGridTextColumn x:Name="AccColumn" Binding="{Binding Path=Acc}" Header="Account" Width="80" IsReadOnly="True" />
                            <DataGridTextColumn x:Name="NameColumn" Binding="{Binding Path=Name}" Header="Name" Width="80" IsReadOnly="True" />
                        </DataGrid.Columns>
                    </DataGrid>
                    <Button Content="Exit" Height="49" HorizontalAlignment="Left" Margin="669,0,0,110" Name="btnExit" VerticalAlignment="Bottom" Width="75" Click="btnExit_Click" />
                </Grid>
            </TabItem>
            <TabItem Header="CLASS" Name="tab2">
                <Grid Height="508" Width="753" DataContext="{StaticResource ClassViewSource}">
                    <DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="514" HorizontalAlignment="Left" 
                                  ItemsSource="{Binding}" Margin="6,2,0,0" Name="ClassDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected"
                                  AlternatingRowBackground="LightBlue" VerticalContentAlignment="Center"
                                  HorizontalGridLinesBrush="Gray" VerticalGridLinesBrush="Gray"
                                  VerticalAlignment="Top" Width="342" SelectedCellsChanged="ClassDataGrid_SelectedCellsChanged">
                        <DataGrid.Columns>
                            <DataGridTextColumn x:Name="ClassNameColumn" Binding="{Binding Path=ClassName}" Header="ClassName" Width="280" />
                        </DataGrid.Columns>
                    </DataGrid>
                    <Button Content="Exit" Height="49" HorizontalAlignment="Left" Margin="672,0,0,178" Name="btnExit2" VerticalAlignment="Bottom" Width="75" Click="btnExit_Click"/>
                </Grid>
            </TabItem>
        </TabControl>

1 个答案:

答案 0 :(得分:2)

最后,我发现我将UserViewSource = ((CollectionViewSource)(this.FindResource("UserViewSource")));放在cs文件中的Window_Loaded函数中。然后,它的工作原理。最初,我将它放入Tab_selectionchanged函数。