2016-07-16 48 views
4

如果InProgressState,那麼我如何獲得GridView來創建State組,並且所有其他選項都沒有任何組?如何在特定條件下將DataGrid中的數據分組?

public class RecordVm: VmBase 
    { 
     public int Id { get; set; } 
     public string Description { get; set; } 
     public State State { get; set; } 
     public bool IsCompeleted { get; set; } 
    } 
    public enum State 
    { 
     Empty, Opened, InProgress, Completed 
    } 

    public class MainVm : VmBase 
    { 
     public ObservableCollection<RecordVm> RecordVms { get; } = new ObservableCollection<RecordVm>(); 

     public ListCollectionView ListCollection {get;} 

     public MainVm() 
     { 
      ListCollection = new ListCollectionView(RecordVms); 
      ListCollection.GroupDescriptions?.Add(new PropertyGroupDescription("State")); 
     } 
    } 

目前,我已經爲每個州的變體創建了一個組,但這樣的選項不適合我。

<DataGrid ItemsSource="{Binding ListCollection}" 
     Style="{StaticResource AzureDataGrid}" 
     RowStyle="{DynamicResource DataGridRowStyleStateGreen}"> 
<DataGrid.GroupStyle> 
    <GroupStyle> 
     <GroupStyle.HeaderTemplate> 
      <DataTemplate> 
       <StackPanel> 
        <TextBlock Text="{Binding Path=Name}" /> 
       </StackPanel> 
      </DataTemplate> 
     </GroupStyle.HeaderTemplate> 
     <GroupStyle.ContainerStyle> 
      <Style TargetType="{x:Type GroupItem}"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type GroupItem}"> 
          <Expander> 
           <Expander.Header> 
            <StackPanel Orientation="Horizontal"> 
             <TextBlock Text="{Binding Path=Name}" /> 
             <TextBlock Margin="5,0,0,0" Text="{Binding Path=ItemCount}"/> 
             <TextBlock Text=" Items"/> 
            </StackPanel> 
           </Expander.Header> 
           <ItemsPresenter /> 
          </Expander> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </GroupStyle.ContainerStyle> 
    </GroupStyle> 
</DataGrid.GroupStyle> 

enter code here 
+0

最簡單的方法可能是,你增加一個屬性到模型中,並通過GRUP那一個,而不是試圖跳過某些羣體 – lokusking

+0

@lokusking,你會不會很難做出一個小例子,我更好地理解你的想法 – Lightness

回答

3

如果我理解正確你,這可能組的方式,你希望它: XAML中保持原樣!

型號

public class RecordVm 
     { 
      public int Id { get; set; } 
      public string Description { get; set; } 
      public State State { 
       get { return this._state; } 
       set { this._state = value; 
        if (value == State.InProgress) 
         this.InProgress = true;return; 
        this.InProgress = false; } 
      } 
      private State _state; 
      public bool IsCompeleted { get; set; } 

      public bool InProgress { get; private set; } 


     } 

轉換

public class DisplayConverter : IValueConverter 
     { 
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
      { 
       if (value == null) return ""; 
       if ((bool) value) return "In Progress"; 
       return "Finished"; 
      } 

      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
      { 
       throw new NotImplementedException(); 
      } 
     } 

使用

ListCollection.GroupDescriptions?.Add(new PropertyGroupDescription("InProgress", new DisplayConverter())); 
2

請看看這樣能否解決您的問題:

ListCollection = new ListCollectionView(RecordVms); 
ListCollection.GroupDescriptions?.Add(new PropertyGroupDescription("State")); 
ListCollection.Refresh(); 

CollectionViewGroup group = (CollectionViewGroup) ListCollection.Groups[0]; 

ListCollectionView viewOfGroup1 = new ListCollectionView(group.Items);    
viewOfGroup1.Filter = ((i) => { return ((RecordVm)i).State == State.InProgress; }); 
viewOfGroup1.Refresh(); 
相關問題