gpt4 book ai didi

silverlight - 如何从 ViewModel 绑定(bind) StackPanel Children?

转载 作者:行者123 更新时间:2023-12-03 10:15:29 26 4
gpt4 key购买 nike

Silverlight 的新手。我正在开发一个聊天应用程序,其中新的聊天消息被添加到列表的底部。我有一个工作版本,在 ScrollViewer 中用作 StackPanel,然后在后面的一些代码中使用 StackPanel.Children.Add()。

我正在尝试将其转换为 View-ViewModel 方法,但我不知道如何将 StackPanel 的 Children 绑定(bind)到任何集合属性。我试过这个:

<ScrollViewer Name="scrollMessages" Grid.Row="2" Margin="0,0,0,0" VerticalScrollBarVisibility="Visible">
<StackPanel x:Name="pnlMessages" Orientation="Vertical" Children="{Binding Path=ExampleTBs}" />
</ScrollViewer>

其中 ExampleTBs 是在代码中创建的 TextBlocks 的集合。 XAML 解析失败,Children 属性不能以这种方式绑定(bind)。

绑定(bind)到 StackPanel 本身的方法是否可以修复?我应该使用不同的容器类型吗?我看到另一个问题,那个人用代码创建了整个 StackPanel,然后使用了 ContentPresenter ...

底线,我想找到一种方法将我的 View 数据绑定(bind)到 View 模型,使用类似 StackPanel 的东西作为容器,随着时间的推移,连续的项目将添加到容器中。最好的方法?

最佳答案

使用 ListBox (或任何其他 ItemsControl )并绑定(bind) ItemsSource 属性到 ObservableCollection 在您的 View 模型中。

关于silverlight - 如何从 ViewModel 绑定(bind) StackPanel Children?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3214549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com