Currently I have a TabControl with several TabItems. Each TabItem has a DataGrid inside. I wanted to format these DataGrids (cell colors, column widths, etc) all at once but I found I can't because all the DataGrids from the hidden tabs would return null properties. In this case, I tried to make a work around where I would select programmatically (or manually with the mouse) the tabs before formatting the DataGrid. But now I'm up against a "strange" behavior:
private void LeftTabs_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Console.WriteLine(LeftTabs.SelectedIndex);
var currentDataGrid = (DataGrid)LeftTabs.SelectedContent;
Console.WriteLine(currentDataGrid.Name);
}
The selected index returns the correct tab index, but the content it's not updated. Let's say Tab 1 is selected and then I click on Tab 2. It returns me the Tab 2 index and the Tab 1 DataGrid name.
This behavior prevents me from editing the select tab's DataGrid because even if I try to access it directly by it's object, all the properties return null.
This is the TabControl, item and DataGrids XAML code:
<TabControl Name="LeftTabs" Margin="0,0,0,0" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" SelectionChanged="LeftTabs_SelectionChanged">
<TabItem>
<TabItem.Header>Conditions</TabItem.Header>
<DataGrid x:Name="DataGrid_Conditions" SelectedCellsChanged="DataGrid_Conditions_SelectedCellsChanged" ColumnWidth="80" ItemsSource="{Binding}" HorizontalAlignment="Stretch" VerticalAlignment="Top" SelectionChanged="ConditionsSelected" />
</TabItem>
<TabItem>
<TabItem.Header>Signals</TabItem.Header>
<DataGrid x:Name="DataGrid_Signals" ColumnWidth="80" ItemsSource="{Binding}" HorizontalAlignment="Stretch" VerticalAlignment="Top" SelectionChanged="SignalsSelected" />
</TabItem>
</TabControl>
EDIT: To be more clear I'll minimize the scenario. TabItem1 - Has DataGrid_Conditions inside; TabItem2 - Has DataGrid_Signals inside.
Here is another code that I try to run when I manually or programmatically select a tab:
DataGridRow Row = (DataGridRow)DataGrid_Signals.ItemContainerGenerator.ContainerFromIndex(ID);
What happens is, If I click on tab2 this code doesn't for tab2's Grid. Instead it works for the previous tab(1) Grid. The LeftTabs.SelectedContent is the only property not being updated.