SwordfishCollections
SwordfishCollections copied to clipboard
want to use Swordfish.NET.CollectionsV3 in WinForms
I use WPF in WinForms . Before , i used CollctionsV2 and modeified the DispathcerQueueProcesser.cs so that it can run in WinForms , but the the UI and backgroundwork often freeze . I can't figure out why it freeze. So i want to use ColltionsV3 , but i don't know how to modify it so that it can be used in WinFroms. Any idea?
Hi @JuliaSong CollectionsV3 doesn't have any WPF specific stuff in it. Doesn't use Dispatcher, or any UI thread related stuff. Instead it has a CollectionView property that WPF based items controls bind to. The CollectionView property changes every time the collection changes as the return value is an Immutable Collection. WPF lets you change properties on a thread that isn't the GUI thread, the only thing it does need to be changed on the GUI thread is ObservableCollection<T> objects. My ConcurrentObservableCollection class gets around this limitation.
I haven't used Winforms since 2005 (.NET 2.0), I started using .NET 3.0 during the beta testing period in 2006. I don't think they had databinding in Winforms back in .NET 2.0, or at least if they did I didn't use it. I guess you could hook the PropertyChanged event on ConcurrentObservableCollection, listen for "CollectionView" property changes, and assign the CollectionView property to your items source on your items control. In Winforms this would have to be done on the UI thread I think, unless they've changed that part of Winforms. I'm not really familiar with Winforms at this point in time.
@JuliaSong for some reason I only got your follow-up comments by e-mail, but they don't appear here. I haven't had much of a chance to look through them because I've been watching a live show at Her Majesty's Theatre, but just quickly before the night is over:
You can use ICollectionView.Filter() with V3 of the collections. I have an example app, that's included in the main Solution file, that I called DataGridGroupSortFilterUltimateExample found here. Tomorrow if I get the chance 🤞 I might do a youtube video on it.
You sent me a code snippet like this:
DataView= CollectionViewSource.GetDefaultView(ConcurrentObservableCollections.CollectionView) ;
As well as that you should have a way of updating the ItemsSource in DataView, so something like this:
ConcurrentObservableCollections.PropertyChanged += (s,e)=>
{
if (e.PropertyName == "CollectionView")
{
DataView.ItemsSource = ConcurrentObservableCollections.CollectionView;
}
}
The reason being is that the CollectionView property is updated to a whole new collection every time the parent collection is modified, it's done using the ImmutableCollection class from Microsoft, so the overhead is actually really low. I'm not familiar with GalaSoft.MvvmLight.ViewModelBase, I'll have to check it out later.
@stewienj Thank you very much! I deleted my comments because i got some idea from your Usage . I did use Propertychanged event to update DataView .
Hi, stewienj . have a WPF Grid Binding to the CollectionView property of the collection, i found the highlight on the row i selected disappeared after a new item added to collection . I think it's because CollectionView property is a new immutable collection every time an item is added, removed or moved. Any idea on how to retain selected on the collection itself ?
Hi @JuliaSong do you have a binding to the selection in your view model? I find having a view model property bound to SelectedValue works in the past. I won't get a chance to test this for about 12 more hours though. Are you using a GridView as the view of a ListView? Or are you using a DataGrid?
@JuliaSong this one's got me stumped, I've added a new example under "ExamplesAndTests", it has a ListView with a GridView for the view, and a DataGrid. I can select a row, then add an item, the selection is retained in the ListView (and the row highlight is retained), the selection is retained in the GridView, but the GridView row isn't highlighted, just the row header on the left is highlighted. So far I haven't been able to work out how to get the whole row highlighted again.
I've done a workaround by changing the style on the DataGrid, here's my DataGrid xaml:
<DataGrid ItemsSource="{Binding TestCollection.CollectionView}" SelectedValue="{Binding SelectedPerson}" AutoGenerateColumns="True">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#FF0078D7"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>
The issue seems to be the Cells lose their selection status, but the row selection is retained. You can see the difference between cell selection and row selection with this example:
<DataGrid ItemsSource="{Binding TestCollection.CollectionView}" SelectedValue="{Binding SelectedPerson}" AutoGenerateColumns="True">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Brown"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#FF0078D7"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>