Issue with bindings "ListBox" to DData SourceList #37
-
I have a ViewModel namespace AvaloniaExample.ViewModels
open Elmish
open ReactiveElmish
open ReactiveElmish.Avalonia
open DynamicData
module Chat =
type Message = { Text: string; Alignment: string; Color: string }
type Model = { Messages: SourceList<Message> }
type Msg =
| SendMessage of Message
let init() =
let initialMessages =
[
{ Text = "Hello, world!"; Alignment = "Left"; Color = "Blue" }
{ Text = "How are you?"; Alignment = "Right"; Color = "Green" }
{ Text = "I'm fine, thank you."; Alignment = "Left"; Color = "Blue" }
{ Text = "That's good to hear!"; Alignment = "Right"; Color = "Green" }
]
{ Messages = SourceList.createFrom initialMessages }
let update (msg: Msg) (model: Model) =
match msg with
| SendMessage message -> model.Messages |> SourceList.add message; model
open Chat
type ChatViewModel() =
inherit ReactiveElmishViewModel()
let app = App.app
let local =
Program.mkAvaloniaSimple init update
|> Program.withErrorHandler (fun (_, ex) -> printfn "Error: %s" ex.Message)
|> Program.mkStore
member this.Messages = local.Model.Messages
static member DesignVM = new ChatViewModel()
and a View <UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="700" d:DesignHeight="650"
xmlns:vm="using:AvaloniaExample.ViewModels"
Design.DataContext="{x:Static vm:ChatViewModel.DesignVM}"
x:DataType="vm:ChatViewModel"
x:Class="AvaloniaExample.Views.ChatView">
<StackPanel HorizontalAlignment="Center" Margin="10">
<ListBox Items="{Binding Messages}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border CornerRadius="5" Padding="10" Margin="5" Background="{Binding Color}" HorizontalAlignment="{Binding Alignment}">
<TextBlock Text="{Binding Text}" />
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</UserControl> And I cannot seem to get them to bind the "Messages" SourceList to the ListBox "Items" - Can you help?
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
You just need to use the |
Beta Was this translation helpful? Give feedback.
-
I think you need to bind to ItemsSource instead of Items. |
Beta Was this translation helpful? Give feedback.
I think you need to bind to ItemsSource instead of Items.
AvaloniaUI/Avalonia#10867