Is it possible to use itemTemplate and dataTemplate
See original GitHub issueI’ve tested adding 1000 items to a stack panel using the naive implementation and the UI grinds. However I spent a bit of time figuring out how to get DataTemplates working in FuncUI way and it seems to work and the UI becomes super snappy even for 1000 element lists.
The code for the working mini app is
https://gist.github.com/bradphelan/06d2e2250facfcf01b848ee71fda4064
The critical line is a helper
let itemTemplate view dispatch =
Avalonia.Controls.Templates.FuncDataTemplate<(int*'state)>( ( fun (id,state) ->
let viewElement = (view state id dispatch)
let view = viewElement |> VirtualDom.createView
let delta = Avalonia.FuncUI.VirtualDom.Delta.ViewDelta.From viewElement
VirtualDom.Patcher.patch(view, delta)
view
) ,true )
and can be used to render lists efficiently like below
module PersonsModule =
type PersonsMsg =
| Update of IndexedMessage<PersonModule.PersonMsg>
| Delete of int
let update (personsMsg:PersonsMsg) state =
match personsMsg with
| Update (id, msg) -> pvSet id (PersonModule.update msg (pvGet id state)) state
| Delete id -> pvDel id state
let itemView (person:PersonModule.PersonState) (id:int) dispatch : View =
// Set up a dispatcher for a person at a specific id
let dispatchPerson id = (fun msg -> dispatch(Update(id,msg)))
Views.dockpanel [
Attrs.children [
Views.button [
Attrs.content "X"
Attrs.onClick ( fun sender args -> dispatch (PersonsMsg.Delete id) )
]
PersonModule.view person (dispatchPerson id)
]
]
let view (state:PersonModule.PersonState PersistentVector) (dispatch) : View =
Views.scrollViewer [
Attrs.content (
Views.listBox [
Attrs.itemTemplate (itemTemplate itemView dispatch )
Attrs.items (state |> Seq.indexed |> PersistentVector.ofSeq )
]
)
]
Maybe I’m telling you something you already know but it seemed like a non-obvious trick and I had to pull some code out of the API to make it work. Hope it is useful for vNext as you think about it.
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Data Templating Overview - WPF .NET Framework
Also, if you are binding to XML data, you wouldn't be able to override ToString . ... ItemTemplate> <DataTemplate> <StackPanel> <TextBlock ...
Read more >ItemsControl with multiple DataTemplates for a viewmodel
is it possible to bind an itemscontrol with canvas as template to multiple DataTemplates? I have 2 collections and depending on the type...
Read more >[Solved] Use DataTemplate for `ItemsControl.ItemsPanel`
Also from what I remember it wasn't possible to invalidate template depending on custom DataTemplateSelector property. Same as in Avalonia.
Read more >FlowLayoutControl ItemTemplate DataTemplate using ...
Hi (FYI: The version of DevExpress I am using is V12.2, there was no way I was able to select that in the...
Read more >ListView, data binding and ItemTemplate
Fortunately, WPF makes all of this very simple using templates. ListView with an ItemTemplate. WPF is all about templating, so specifying a data...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Awsome 😃
On Fri., 4 Oct. 2019, 20:19 Josua Jäger, notifications@github.com wrote:
I now have a working prototype!
backing data is a list from 1 to 100.000