WPF数据网格无法选择行
本文关键字:选择 网格 数据 数据网 WPF | 更新日期: 2023-09-27 18:11:22
我使用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>
最后,我发现我把我的UserViewSource = ((CollectionViewSource)(this.FindResource("UserViewSource")));
在cs文件到Window_Loaded函数。然后,它就起作用了。原来,我把它放在Tab_selectionchanged
函数中。