2016-08-19 43 views
1

嗨有一個Datagrid的組,我添加了一個按鈕來展開所有組,但它不工作,所有組保持摺疊。wpf datagrid展開全部不工作

我使用PropertyChanged事件處理程序,並用命令

下面的按鈕是XAML:

 <StackPanel Grid.Row="0"> 
     <Button x:Name="ExpandAll" Content="Tout deplier" VerticalAlignment="Bottom" Command="{Binding ExpandAll}"/> 
     <!-- This textblock text is updated by the Expanded property changed --> 
     <TextBlock Text="{Binding Expanded}" /> 
    </StackPanel> 
    <DataGrid x:Name="GrdLignes" HorizontalAlignment="Stretch" VerticalContentAlignment="Stretch" Margin="0,0,0,0" 
       Grid.Row="1" VerticalAlignment="Top" AutoGenerateColumns="False" CanUserAddRows="False" 
       CanUserDeleteRows="False" ItemsSource="{Binding Lignes}" IsReadOnly="True" 
       RowDetailsVisibilityMode="VisibleWhenSelected" RowHeaderWidth="25"> 
     <DataGrid.GroupStyle> 
      <GroupStyle> 
       <GroupStyle.ContainerStyle> 
        <Style TargetType="{x:Type GroupItem}"> 
         <Setter Property="Template"> 
          <Setter.Value> 
           <ControlTemplate TargetType="{x:Type GroupItem}"> 
            <Expander Background="Lavender" IsExpanded="{Binding Expanded}"> 
             <Expander.Header> 
              <StackPanel Orientation="Horizontal"> 
               <TextBlock Text="{Binding Path=Name}" Padding="0,0,5,0" FontWeight="Bold" /> 
               <TextBlock Text="{Binding Path=ItemCount}" Padding="0,0,5,0"/> 
               <TextBlock Text="Commandes"/> 
              </StackPanel> 
             </Expander.Header> 
             <ItemsPresenter /> 
            </Expander> 
           </ControlTemplate> 
          </Setter.Value> 
         </Setter> 
        </Style> 
       </GroupStyle.ContainerStyle> 
      </GroupStyle> 
     </DataGrid.GroupStyle> 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="Pièce Achat" Binding="{Binding Path=Piece}" FontWeight="Bold"/> 
      <DataGridTextColumn Header="Type" Binding="{Binding Path=TypeLabel}"> 
       <DataGridTextColumn.ElementStyle> 
        <Style TargetType="{x:Type TextBlock}"> 
         <Setter Property="Background" Value="{Binding Path=Type, Converter={StaticResource TypeToBrushConverter}}" /> 
        </Style> 
       </DataGridTextColumn.ElementStyle> 
      </DataGridTextColumn> 
      <DataGridTextColumn Header="Statut" Binding="{Binding Path=StatutLabel}"> 
       <DataGridTextColumn.ElementStyle> 
        <Style TargetType="{x:Type TextBlock}"> 
         <Setter Property="Background" Value="{Binding Path=Statut, Converter={StaticResource StatutToBrushConverter}}" /> 
        </Style> 
       </DataGridTextColumn.ElementStyle> 
      </DataGridTextColumn> 

     </DataGrid.Columns> 
     <DataGrid.RowDetailsTemplate> 
      <DataTemplate> 
       <DataGrid RowHeaderWidth="25" ItemsSource="{Binding Path=Lignes}" AutoGenerateColumns="False" Margin="0" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True"> 
        <DataGrid.Columns> 
         <DataGridTextColumn Header="Acheteur" Binding="{Binding Path=Acheteur}"/> 
         <DataGridTextColumn Header="Pièce" Binding="{Binding Path=Piece}"/> 
         <DataGridTextColumn Header="Client" Binding="{Binding Path=Client}"/> 
         <DataGridTextColumn Header="Ref" Binding="{Binding Path=ArRef}"/> 
         <DataGridTextColumn Header="Ref Fourn" Binding="{Binding Path=RefFourn}"/> 
         <DataGridTextColumn Header="Designation" Binding="{Binding Path=Designation}"/> 
         <DataGridTextColumn Header="Qte" Binding="{Binding Path=CmQte}"/> 
         <DataGridTextColumn Header="Vendeur" Binding="{Binding Path=Vendeur}"/> 
        </DataGrid.Columns> 
       </DataGrid> 
      </DataTemplate> 
     </DataGrid.RowDetailsTemplate> 
    </DataGrid> 

這裏是視圖模型:

public class MainViewModel : INotifyPropertyChanged 
{ 
    private bool _expanded = false; 

    public bool Expanded 
    { 
     get { return _expanded; } 
     set 
     { 
      _expanded = value; 
      OnPropertyChanged("Expanded"); 
     } 
    } 

    public ICommand ExpandAll { get; set; } 

    public MainViewModel() 
    { 
     ExpandAll = new Command(ExpandAllAction); 
    } 

    private void ExpandAllAction(object parameters) 
    { 
     Expanded = true; 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void OnPropertyChanged(string propertyName) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

回答

0

我發現從該線程的溶液: https://stackoverflow.com/a/12611779/3182178

我在MainWindow類添加以下代碼:

public static class VisualTreeHelper 
    { 
     public static Collection<T> GetVisualChildren<T>(DependencyObject current) where T : DependencyObject 
     { 
      if (current == null) 
       return null; 

      var children = new Collection<T>(); 
      GetVisualChildren(current, children); 
      return children; 
     } 
     private static void GetVisualChildren<T>(DependencyObject current, Collection<T> children) where T : DependencyObject 
     { 
      if (current != null) 
      { 
       if (current.GetType() == typeof(T)) 
        children.Add((T)current); 

       for (int i = 0; i < System.Windows.Media.VisualTreeHelper.GetChildrenCount(current); i++) 
       { 
        GetVisualChildren(System.Windows.Media.VisualTreeHelper.GetChild(current, i), children); 
       } 
      } 
     } 
    } 

    private void ExpandAll_OnClick(object sender, RoutedEventArgs e) 
    { 
     Collection<Expander> collection = VisualTreeHelper.GetVisualChildren<Expander>(GrdLignes); 

     foreach (Expander expander in collection) 
      expander.IsExpanded = true; 
    } 

    private void CollapseAll_OnClick(object sender, RoutedEventArgs e) 
    { 
     Collection<Expander> collection = VisualTreeHelper.GetVisualChildren<Expander>(GrdLignes); 

     foreach (Expander expander in collection) 
      expander.IsExpanded = false; 
    } 

然後,XAML內部我添加了兩個按鈕,這個代碼:

<Button Name="ExpandAll" Content="++" VerticalAlignment="Bottom" Width="30" Click="ExpandAll_OnClick"/> 
<Button Name="CollapseAll" Content="--" VerticalAlignment="Bottom" Width="30" Margin="0" Click="CollapseAll_OnClick"/> 

這不是最好的,但它的工作...

1

我認爲你需要綁定到擴展屬性時將UpdateSource觸發器設置爲「PropertyChanged」。

<Expander Background="Lavender" IsExpanded="{Binding Expanded, UpdateSourceTrigger=PropertyChanged}"> 
            <Expander.Header> 
             <StackPanel Orientation="Horizontal"> 
              <TextBlock Text="{Binding Path=Name}" Padding="0,0,5,0" FontWeight="Bold" /> 
              <TextBlock Text="{Binding Path=ItemCount}" Padding="0,0,5,0"/> 
              <TextBlock Text="Commandes"/> 
             </StackPanel> 
            </Expander.Header> 
            <ItemsPresenter /> 
           </Expander> 
+0

我已經試過這個,但它仍然無法正常工作 – ebelair