Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Multiple CollectionView with different filters on same collection
    primarykey
    data
    text
    <p>I'm using a an <code>ObservableCollection</code> with two <code>ICollectionView</code> for different filters.</p> <p>One is for filtering messages by some type, and one is for counting checked messages. As you can see message filter and message count works OK, but when I'm un-checking the message disappear from the list (<em>the count is still working</em>).</p> <p>BTW sorry for the long post, I wanted to include all relevant stuff.</p> <p><strong>The XAML Code:</strong></p> <pre><code>&lt;!-- Messages List --&gt; &lt;DockPanel Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Height="500"&gt; &lt;ListBox Name="listBoxZone" ItemsSource="{Binding filteredMessageList}" Background="Transparent" BorderThickness="0"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;CheckBox Name="CheckBoxZone" Content="{Binding text}" Tag="{Binding id}" Unchecked="CheckBoxZone_Unchecked" Foreground="WhiteSmoke" Margin="0,5,0,0" IsChecked="{Binding isChecked}" /&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/DockPanel&gt; &lt;Button Content="Test Add New" Grid.Column="2" Height="25" HorizontalAlignment="Left" Margin="34,2,0,0" Click="button1_Click" /&gt; &lt;Label Content="{Binding checkedMessageList.Count}" Grid.Column="2" Height="25" Margin="147,2,373,0" Width="20" Foreground="white" /&gt; </code></pre> <p><strong>Screenshot:</strong> <img src="https://i.stack.imgur.com/cVYM6.gif" alt="enter image description here"></p> <p><strong>Code:</strong></p> <pre><code>/* ViewModel Class */ public class MainViewModel : INotifyPropertyChanged { // Constructor public MainViewModel() { #region filteredMessageList // connect the ObservableCollection to CollectionView _filteredMessageList = CollectionViewSource.GetDefaultView(messageList); // set filter _filteredMessageList.Filter = delegate(object item) { MessageClass temp = item as MessageClass; if ( selectedFilter.Equals(AvailableFilters.All) ) { return true; } else { return temp.filter.Equals(_selectedFilter); } }; #endregion #region checkedMessageList // connect the ObservableCollection to CollectionView _checkedMessageList = CollectionViewSource.GetDefaultView(messageList); // set filter _checkedMessageList.Filter = delegate(object item) { return (item as MessageClass).isChecked; }; #endregion } // message List private ObservableCollection&lt;MessageClass&gt; _messageList = new ObservableCollection&lt;MessageClass&gt;(); public ObservableCollection&lt;MessageClass&gt; messageList { get { return _messageList; } set { _messageList = value; } } // CollectionView (filtered messageList) private ICollectionView _filteredMessageList; public ICollectionView filteredMessageList { get { return _filteredMessageList; } } // CollectionView (filtered messageList) private ICollectionView _checkedMessageList; public ICollectionView checkedMessageList { get { return _checkedMessageList; } } // SelectedFilter property private AvailableFilters _selectedFilter = AvailableFilters.All; // Default is set to all public AvailableFilters selectedFilter { get { return _selectedFilter; } set { _selectedFilter = value; RaisePropertyChanged("selectedFilter"); _filteredMessageList.Refresh(); // refresh list upon update } } // FilterList (Convert Enum To Collection) private List&lt;KeyValuePair&lt;string, AvailableFilters&gt;&gt; _AvailableFiltersList; public List&lt;KeyValuePair&lt;string, AvailableFilters&gt;&gt; AvailableFiltersList { get { /* Check if such list available, if not create for first use */ if (_AvailableFiltersList == null) { _AvailableFiltersList = new List&lt;KeyValuePair&lt;string, AvailableFilters&gt;&gt;(); foreach (AvailableFilters filter in Enum.GetValues(typeof(AvailableFilters))) { string Description; FieldInfo fieldInfo = filter.GetType().GetField(filter.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); /* if not null get description */ if (attributes != null &amp;&amp; attributes.Length &gt; 0) { Description = attributes[0].Description; } else { Description = string.Empty; } /* add as new item to filterList */ KeyValuePair&lt;string, AvailableFilters&gt; TypeKeyValue = new KeyValuePair&lt;string, AvailableFilters&gt;(Description, filter); _AvailableFiltersList.Add(TypeKeyValue); } } return _AvailableFiltersList; } } #region Implement INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; public void RaisePropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } #endregion } </code></pre> <p><strong>Code For un-check function</strong></p> <pre><code>private void CheckBoxZone_Unchecked(object sender, RoutedEventArgs e) { CheckBox chkZone = (CheckBox)sender; ucSystemMessageVM.checkedMessageList.Refresh(); } </code></pre>
    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.
 

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