2010-11-21 17 views
14

Neyi yanlış yaptığımı anlayamıyorum. ListView'de öğeleri gruplamak istiyorum. Sonuç olarak Böyle bir şey görmek istiyorum: MVVM ListView'de Öğeleri Gruplama

enter image description here

It'm

MVVM desen kullanarak. Bu benim XAML kodum. Ben Expander.Headerile özelliği "FirstName" bağlamak olamaz Neden

<CollectionViewSource x:Key="EmploeeGroup"        
         Source="{Binding Path=AllEmploees}"> 
    <CollectionViewSource.GroupDescriptions> 
    <PropertyGroupDescription PropertyName="FirstName" /> 
    </CollectionViewSource.GroupDescriptions> 
</CollectionViewSource> 

<ListView AlternationCount="2" 
      DataContext="{StaticResource EmploeeGroup}" 
      ItemsSource="{Binding IsAsync=True}" Padding="0,0,0,10"> 
    <ListView.GroupStyle> 
    <GroupStyle> 
     <GroupStyle.ContainerStyle> 
     <Style TargetType="{x:Type GroupItem}"> 
      <Setter Property="Margin" Value="0,0,0,5"/> 
      <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type GroupItem}"> 
       <Expander IsExpanded="True" BorderBrush="#FFA4B97F" 
              BorderThickness="0,0,0,1"> 
        <Expander.Header> 
        <DockPanel> 
         <TextBlock FontWeight="Bold" 
           Text="Name: "/> 
         <TextBlock FontWeight="Bold" 
           Text="{Binding Path=FirstName}"/> 
        </DockPanel> 
        </Expander.Header> 
        <Expander.Content> 
        <ItemsPresenter /> 
        </Expander.Content> 
       </Expander> 
       </ControlTemplate> 
      </Setter.Value> 
      </Setter> 
     </Style> 
     </GroupStyle.ContainerStyle> 
    </GroupStyle> 
    </ListView.GroupStyle> 
    <ListView.View> 
    <GridView> 
     <GridViewColumn Width="150" 
         Header="FirstName" 
         DisplayMemberBinding="{Binding Path=FirstName}"/> 
     <GridViewColumn Width="150" 
         Header="LastName" 
         DisplayMemberBinding="{Binding Path=LastName}"/> 
    </GridView> 
    </ListView.View> 
</ListView> 

Benim EmploeeListViewModel.cs

public class EmploeeListViewModel: ViewModelBase 
{ 
    readonly EmploeeRepository _emploeeRepository; 

    private ObservableCollection<EmploeeViewModel> _allmpl; 
    public ObservableCollection<EmploeeViewModel> AllEmploees 
    { 
    get 
    { 
     if (_allmpl == null) 
     { 
     _allmpl = new ObservableCollection<EmploeeViewModel>(); 
     CreateAllEmploee(); 
     } 
     return _allmpl; 
    } 
    } 

    public EmploeeListViewModel(EmploeeRepository emploeeRepository) 
    { 
    if (emploeeRepository == null) 
     throw new ArgumentNullException("emploeeRepository"); 

    _emploeeRepository = emploeeRepository; 
    _emploeeRepository.EmploeeAdded += this.OnEmploeeAddedToRepository; 
    } 

private void CreateAllEmploee() 
{ 
    List<EmploeeViewModel> all = 
       (from emploee in _emploeeRepository.GetEmploees() 
       select new EmploeeViewModel(emploee)).ToList(); 
    foreach (EmploeeViewModel evm in all) 
    { 
    evm.PropertyChanged += this.OnEmploeeViewModelPropertyChanged; 
    AllEmploees.Add(evm); 
    } 
    this.AllEmploees.CollectionChanged += this.OnCollectionChanged; 
} 

//this.OnCollectionChanged; 
//this.OnEmploeeViewModelPropertyChanged; 
} 

EmploeeViewModel.cs

public class EmploeeViewModel : ViewModelBase 
{ 
    #region Fields 
    Emploee _emploee; 
    bool _isSelected; 
    #endregion 

    #region Constructor 
    public EmploeeViewModel(Emploee emploee) 
    { 
     if (emploee == null) 
     throw new ArgumentNullException("emploee"); 
     this._emploee = emploee; 
    } 
    #endregion 

    #region Emploee Properties 
    public bool IsSelected 
    { 
     get { return _isSelected; } 
     set 
     { 
     if (value == _isSelected) 
      return; 

     _isSelected = value; 
     base.OnPropertyChanged("IsSelected"); 
     } 
    } 

    public string FirstName 
    { 
     get { return _emploee.FirstName; } 
     set 
     { 
     if (value == _emploee.FirstName) 
      return; 
     _emploee.FirstName = value; 
     base.OnPropertyChanged("FirstName"); 
     } 
    } 

    public string LastName 
    { 
     get { return _emploee.LastName; } 
     set 
     { 
     if (value == _emploee.LastName) 
      return; 
     _emploee.LastName = value; 
     base.OnPropertyChanged("LastName"); 
     } 
    } 
    #endregion 
} 
  • varTextBlock?
  • Neden (i Expander.Header Metin içindeki yazdıysam = "{Binding}") Ben Expander.Header içindeki tip
    MS.Internal.Data.CollectionViewGroupInternal nesne var>?

nasıl XAML veya .cs kod these results üretmek için değiştirmek gerekir?

cevap

20

Bu soruya kendi başıma cevap buldum.

Dönüştürücüye gönderilen nesne, tür: MS.Internal.Data.CollectionViewGroupInternal.

temel nedeni olduğunu (eğer belirtilen GroupDescription göre) geçerli "grubu koleksiyonu" olduğunu adını içeren CollectionViewGroupInternal mülk olduğu için basitçe grup adlarını veri bağlama için "Ad" kullanmaktır.

Önemli değil PropertyGroupDescription'da GropertyName öğesi neydi? her zaman GroupStyle kapsayıcısında {Binding Path = Name} kullanmanız gerekir.

XAML'imde yalnızca bir dizgi değiştirmek zorunda kaldım.

<TextBlock FontWeight="Bold" Text="{Binding Path=FirstName}"/> 

için: Nereden

Birden PropertyGroupDescriptions ile bunun nasıl yapılacağı merak

<TextBlock FontWeight="Bold" Text="{Binding Path=Name}"/> 
+0

... – Sven

+1

Bu son derece çarşı. Bu yazıyı http://www.wpf-tutorial.com/listview-control/listview-grouping/ adresinde fark ettim ve bunun bir yazım hatası olabileceğini düşündüm. Neden yeryüzünde her zaman "İsim" olmalı ama asıl bağlayıcı ismi olmamalı !!! Şerefe arkadaşlar. – Mehrad

+1

"Ad", gruplandırdığınız öğeyi tutan mülkün adıdır. Bir FirstName paylaşan ve "Name" özelliğinin ortak değeri olan bir öğe grubunuz var. Karmaşık bir türde gruplandırdığınızda bu daha belirgin hale gelir. –