oxyplot icon indicating copy to clipboard operation
oxyplot copied to clipboard

Reusing plot models in a collection and item templates

Open GeertvanHorrik opened this issue 10 years ago • 5 comments

I have a situation where I have a collection of plot models. I create plot models and show them all by default, but then I also want to filter the collection.

When I filter the same collection (thus reapply the same models again), I get the exception that the model is already attached to another view.

Is there a way to disconnect a model from a view? This is especially useful when using item templates that will be reused after filtering a collection.

GeertvanHorrik avatar Dec 01 '14 09:12 GeertvanHorrik

Could this be reated to/duplicate of #40?

objorke avatar Dec 01 '14 11:12 objorke

Not sure, it's showing the right data, but because it's using item templates after filter, it uses a new one for the same data. At that stage, I need to disconnect the view. I think a great place to unhook the view is in the Unloaded event of the view.

GeertvanHorrik avatar Dec 01 '14 11:12 GeertvanHorrik

I have same(or similar) problem.

I think that this problem relate to the virtualization. In my case, the exception always cause when the virtualization is ON and I move scrollbar with mouse wheel or drug. But it will be a few times when I set VirtualizingPanel.VirtualizationMode="Recycling" to ListBox.

when I set that property, I found there...

  • It seems to cause in start and end of ListBoxItem.
  • It never doesn't cause in the first time scrolling.
  • It will be a few times when the ListBoxItem is many.

However I'm not found the cause yet. I want help.

ghost avatar May 08 '19 08:05 ghost

can you give me a example of connecting ListBox.ItemsSource to a list of PlotView? Because I have a problem when I want display lots of PlotView in a ListBox. Thank you so much!@siksmtt

whispor avatar Jul 01 '21 04:07 whispor

@whispor I use this code with MVVM model. But this is used in real application so I can't give you the details, sorry.

View

<ListBox ItemsSource="{Binding GraphList}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <VirtualizingStackPanel>
                <DockPanel Margin="7,3,7,3" VerticalAlignment="Center">
                    <oxy:PlotView  Height="140" Margin="15,0,20,0" MinWidth="400" HorizontalAlignment="Stretch"
                                   Model="{Binding Chart}" Controller="{Binding Controller}"
                                   Background="{Binding Path=Background, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"/>
                </DockPanel>
            </VirtualizingStackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ListBox>

ViewModel / Model

public ObservableCollection<Graph> GraphList= new ObservableCollection<Graph>();
public PlotModel Chart { get; set; } = new OxyPlotExceptionHelper();  // => Graph Class
//Don't forget  PropertyChanged Notification

The OxyPlotExceptionHelper class is here. You can solve this issue if you use the OxyPlotExceptionHelper class.

I don't know your problem's detail so my answer may be not enougth.

ghost avatar Jul 01 '21 06:07 ghost