2010-11-21 27 views
14

私が間違っていることを理解できません。 listViewに項目をグループ化したいMVVMパターンを使用してListViewのMVVMグループ化アイテム

enter image description here

It'm: は結果では、私はそのような何かを見たいと思っています。それは私のXAMLコードです。

<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> 

それは私のEmploeeListViewModel.cs私は "姓" Expander.Headerと プロパティをバインドすることはできませんなぜ

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 
} 
  • だ TextBlock?私は(iは Expander.Headerテキストの内側に書いた場合= "{}バインディング")Expander.Header内部MS.Internal.Data.CollectionViewGroupInternal タイプ
    オブジェクトた理由
  • >?

にはどうすれば these resultsを生成するために私のXAMLまたは.CSコードを変更する必要があります ?

答えて

20

私はこの質問に対して自分で答えを見つけました。

コンバータに送信されるオブジェクトの種類は、MS.Internal.Data.CollectionViewGroupInternalです。

主な理由は、グループ名をデータバインドするために "名前"を使用することです。これは、現在の "グループコレクション"(指定したGroupDescriptionに従って)の名前を含むCollectionViewGroupInternalのプロパティだからです。

重要でないPropertyGroupDescriptionのGropertyNameは何ですか? 常にです。{Binding Path = Name}をGroupStyleコンテナに使用する必要があります。

XAMLで文字列を1つだけ変更する必要がありました。

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

へ:から

私は複数のPropertyGroupDescriptionsでこれを行う方法を疑問に思う

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

... – Sven

+1

これは非常にバザーです。私はこのポストhttp://www.wpf-tutorial.com/listview-control/listview-grouping/にそれを気付き、これが誤字かもしれないと思った。なぜ地球上でそれは常に "名前"であるべきですが、実際の拘束の名前ではありません!歓声メイト。 – Mehrad

+1

"名前"は、グループ化したアイテムを保持するプロパティの名前です。 FirstNameを共有するアイテムのグループがあり、 "Name"プロパティが共通の値を保持しています。これは、複合型でグループ化するとより明白になります。 –

関連する問題