0

I want to make ItemsControl and CollectionViewSource working together, but despite my research I can not do it.

I want to make different datagrid from my data like this.

     Machine1                                Machine2
--------------------                   -------------------    
Path | Value | Time                    Path | Value | Time 
--------------------                   -------------------
item |  45   | 02:05                   i2   | azeaz | 01:05
item2| ae ea | 02:04                   i3   |  4225 | 02:05

I have a Dictionary which will contain for exemple Machine1 as key and a list of Tag in value (and a tag is an object with my path / value / time)

private Dictionary<string, ItemsChangeObservableCollection<TagViewModel>> _dicoTags;

I want to sort this data in XAML and not in code-behind or in my code. I only generate by data in my code but I want to sort this in datagrid.

My main problem is : each time the datagrid is updated, all my lines are pushed in the row above and the first row is pushed at the bottom, so a human can't read my datagrids because for exemple i2 will never be in the same place (and I want i2 don't moove and just his value and timestamp udpate).

My code do that because I use a custom ObservableCollection which trigger events on update (and then it remove my oldItem and add the new one instead of searching and remplacing item in my observable collection for performance and then if it remove it, it also remove it from my view...) so in my point of view, if I always sort my list with the same method, all lines will be in the same place and will not moove and I will just see my values updated without glitching.

So, after some research, I think using a CollectionViewSource is a good idea to sort my list but I can't make it working yet. I tried this topic for creating collection view source in xaml

I also search to directly sort my main collection in my business code but I think it's better to do it in XAML in my case.

I just let here the code of my view in XAML because all working fine without sorting and I just want to make ItemsControl and CollectionViewSource working together.

<ItemsControl x:Name="itemControlTags" 
        ItemsSource="{Binding CurrentModuleItem.DicoTags}"  
        >
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal"  
                Background="White"
                Width="{Binding ActualWidth, ElementName=itemControlTags}" 
                Height="{Binding ActualHeight, ElementName=itemControlTags}"  />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>

            <ItemsControl.Resources>
                <CollectionViewSource x:Key="ViewSourceList" Source="{Binding Value?????}">
                    <CollectionViewSource.SortDescriptions>
                        <scm:SortDescription PropertyName="OPCPath"/>
                    </CollectionViewSource.SortDescriptions>
                </CollectionViewSource>
            </ItemsControl.Resources>

            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="20">
                        <TextBlock Text="{Binding Key}"></TextBlock>
                        <DataGrid AutoGenerateColumns="False" 
                          CanUserAddRows="False" 
                          IsReadOnly="True" 
                          CanUserDeleteRows="False" 
                          Name="TagsDatagrid"
                          ItemsSource="{Binding Source={StaticResource ViewSourceList}}">
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="OPC Path" Binding="{Binding Path=OPCPath, Mode=TwoWay}"></DataGridTextColumn>
                                <DataGridTextColumn Header="Value" Binding="{Binding Path=Value, Mode=TwoWay}"></DataGridTextColumn>
                                <DataGridTextColumn Header="Timestamp" Binding="{Binding Path=FormatedTimestamp, Mode=OneWay}"></DataGridTextColumn>
                                <DataGridTextColumn Header="Quality" Binding="{Binding Path=Quality, Mode=TwoWay}">
                                    <DataGridTextColumn.ElementStyle>
                                        <Style TargetType="{x:Type TextBlock}">
                                            <Style.Triggers>
                                                <Trigger Property="Text" Value="Good">
                                                    <Setter Property="Background" Value="LightGreen"/>
                                                </Trigger>
                                                <Trigger Property="Text" Value="Bad">
                                                    <Setter Property="Background" Value="Red"/>
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </DataGridTextColumn.ElementStyle>
                                </DataGridTextColumn>
                            </DataGrid.Columns>
                        </DataGrid>
                    </StackPanel>

                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>

Don't hesitate to indicate me other way to achive my objectiv. Thank you

Karakayn
  • 159
  • 1
  • 4
  • 16
  • I'm sadly not the best on WPF, I have deleted my answer as not helpful, I guess you already take a look on it, but here is another link https://stackoverflow.com/questions/23006286/wpf-sorting-itemscontrol-under-datatemplate – Nekeniehl Feb 14 '18 at 13:38
  • Did you bind the Source property of ViewSourceList to your source collection in the view model? – mm8 Feb 14 '18 at 16:26
  • I tried using SortDirection="Ascending" directly as attribute of DataGridTextColumn. There are the header of the column with an arrow like the sort is working but no items was moved. If I click on the header, they are correctly sorted but I can't do it programmatically ... – Karakayn Feb 14 '18 at 16:32

0 Answers0