In WPF, I am using a toggle button in datagrid row header to show its detailed visibility of each parent datagrid row as a child datagrid. When I click a toggle button, the visualstate of the corresponding parent datagrid row will change to "Checked" state. It is working properly. But at same time I wanted to make visualstate of all other parent datagrid row visual state as "Unchecked" or "Normal" without clicking on each toggle button. I am using the following code. But its not working properly. But if I click on each toggle button, then its visual state is changing properly.
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
DependencyObject obj = (DependencyObject)e.OriginalSource;
while (!(obj is DataGridRow) && (obj != null))
{
obj = VisualTreeHelper.GetParent(obj);
}
if (obj is DataGridRow)
{
if ((obj as DataGridRow).DetailsVisibility == Visibility.Visible)
{
(obj as DataGridRow).IsSelected = false;
(obj as DataGridRow).DetailsVisibility = System.Windows.Visibility.Collapsed;
}
else if ((obj as DataGridRow).DetailsVisibility == Visibility.Collapsed)
{
for (int i = 0; i < Data_Grid.Items.Count; i++)
{
DataGridRow itm = GetDataGridRowitem(i);
itm.IsSelected = false;
itm.DetailsVisibility = System.Windows.Visibility.Collapsed;
VisualStateManager.GoToElementState(itm, "Unchecked", true);
}
DataTemplate dt = FindResource("tocchild") as DataTemplate;
Data_Grid.RowDetailsTemplate = dt;
(obj as DataGridRow).IsSelected = true;
(obj as DataGridRow).DetailsVisibility = Visibility.Visible;
VisualStateManager.GoToElementState((obj as DataGridRow), "Checked", true);
}
}}
Also I tried,
VisualStateManager.GoToState((obj as DataGridRow), "Checked", true);
Help me please. Thanks.