2

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.

4

3 回答 3

2

将您的第一列设置datagrid为此

 <DataGridTemplateColumn CellTemplate="{StaticResource ExpandDetails}" Width="30" />

并将其放入dataTemplate资源中

<DataTemplate x:Key="ExpandDetails">
            <ToggleButton Name="ShowDetails" Height="20"
                              IsChecked="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, 
                                                  Path=DetailsVisibility, 
                                                  Converter={StaticResource VisbilityToBooleanConverter}, 
                                                  Mode=TwoWay}" 
                              Margin="4" HorizontalAlignment="Right" FlowDirection="LeftToRight" BorderThickness="0" />
</DataGridTemplateColumn>

然后VisbilityToBooleanConverter像这样创建

public class VisbilityToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (Visibility)value == Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (bool)value ? Visibility.Visible : Visibility.Collapsed;
    }
}
于 2015-01-20T08:16:56.207 回答
1

尝试这个,

class VisibilityConverter : IValueConverter

改变视觉状态

于 2015-01-20T10:38:52.000 回答
0

我通过将值 Toggle Button IsChecked 设置为 True 和 false 来解决它。

FrameworkElement tb = GetTemplateChildByName(row, "RowHeaderToggleButton");
                    (tb as ToggleButton).IsChecked = false;

GetTemplateChildByName 在哪里,

public static FrameworkElement GetTemplateChildByName(DependencyObject parent, string name)
{
    int childnum = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < childnum; i++)
    {
        var child = VisualTreeHelper.GetChild(parent, i);
        if (child is FrameworkElement &&

        ((FrameworkElement)child).Name == name)
        {
            return child as FrameworkElement;
        }
        else
        {
            var s = GetTemplateChildByName(child, name);
            if (s != null)
                return s;
        }
    }
    return null;
}

谢谢大家。

于 2015-01-21T04:31:04.070 回答