Note that there are some explanatory texts on larger screens.

plurals
  1. POMVVM Grouping Items in ListView
    primarykey
    data
    text
    <p>I cannot understand what I'm doing wrong. I want to group items in listView. In result I want to see something like that:</p> <p><img src="https://i.stack.imgur.com/9zF5o.jpg" alt="enter image description here"></p> <p>It'm using MVVM pattern. It's my XAML code.</p> <pre><code>&lt;CollectionViewSource x:Key="EmploeeGroup" Source="{Binding Path=AllEmploees}"&gt; &lt;CollectionViewSource.GroupDescriptions&gt; &lt;PropertyGroupDescription PropertyName="FirstName" /&gt; &lt;/CollectionViewSource.GroupDescriptions&gt; &lt;/CollectionViewSource&gt; &lt;ListView AlternationCount="2" DataContext="{StaticResource EmploeeGroup}" ItemsSource="{Binding IsAsync=True}" Padding="0,0,0,10"&gt; &lt;ListView.GroupStyle&gt; &lt;GroupStyle&gt; &lt;GroupStyle.ContainerStyle&gt; &lt;Style TargetType="{x:Type GroupItem}"&gt; &lt;Setter Property="Margin" Value="0,0,0,5"/&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type GroupItem}"&gt; &lt;Expander IsExpanded="True" BorderBrush="#FFA4B97F" BorderThickness="0,0,0,1"&gt; &lt;Expander.Header&gt; &lt;DockPanel&gt; &lt;TextBlock FontWeight="Bold" Text="Name: "/&gt; &lt;TextBlock FontWeight="Bold" Text="{Binding Path=FirstName}"/&gt; &lt;/DockPanel&gt; &lt;/Expander.Header&gt; &lt;Expander.Content&gt; &lt;ItemsPresenter /&gt; &lt;/Expander.Content&gt; &lt;/Expander&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/GroupStyle.ContainerStyle&gt; &lt;/GroupStyle&gt; &lt;/ListView.GroupStyle&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Width="150" Header="FirstName" DisplayMemberBinding="{Binding Path=FirstName}"/&gt; &lt;GridViewColumn Width="150" Header="LastName" DisplayMemberBinding="{Binding Path=LastName}"/&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; </code></pre> <p>It's my <strong>EmploeeListViewModel.cs</strong></p> <pre><code>public class EmploeeListViewModel: ViewModelBase { readonly EmploeeRepository _emploeeRepository; private ObservableCollection&lt;EmploeeViewModel&gt; _allmpl; public ObservableCollection&lt;EmploeeViewModel&gt; AllEmploees { get { if (_allmpl == null) { _allmpl = new ObservableCollection&lt;EmploeeViewModel&gt;(); CreateAllEmploee(); } return _allmpl; } } public EmploeeListViewModel(EmploeeRepository emploeeRepository) { if (emploeeRepository == null) throw new ArgumentNullException("emploeeRepository"); _emploeeRepository = emploeeRepository; _emploeeRepository.EmploeeAdded += this.OnEmploeeAddedToRepository; } private void CreateAllEmploee() { List&lt;EmploeeViewModel&gt; 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; } </code></pre> <p><strong>EmploeeViewModel.cs</strong></p> <pre><code>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 } </code></pre> <ul> <li>Why can not I bind "FirstName" property with Expander.Header TextBlock? </li> <li>Why have I object type<br> MS.Internal.Data.CollectionViewGroupInternal inside Expander.Header(if i wrote inside Expander.Header Text="{Binding}")>?</li> </ul> <p>How should I change my XAML or .CS code to produce <a href="https://i.stack.imgur.com/9zF5o.jpg" rel="noreferrer">these results</a>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload