动态更新 DataTemplate 的源

发布于 2024-11-06 19:09:23 字数 1571 浏览 0 评论 0原文

大家好,我正在 silverlight 中编写一个 Windows Phone 应用程序,并且我正在尝试动态更新现有的 DataTemplate 源。这是我拥有的 xaml:

    <ListBox Grid.Row="1" Height="607" HorizontalAlignment="Left" Name="listBox1" VerticalAlignment="Top" Width="480">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Height="132">
                    <StackPanel Width="370">
                        <TextBlock Text="{Binding Transcription}" Foreground="#FFC8AB14" FontSize="28" />
                        <TextBlock Text="{Binding Duration}" TextWrapping="Wrap" FontSize="24" />
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

在其背后的 C# 中,我在主 init 函数中传递数据,如下所示:

        list.Add(new NoteToSelf { Transcription = "oh hi", Duration = "9001 seconds" });
        list.Add(new NoteToSelf { Transcription = "fgsfds", Duration = "$Texas seconds" });
        listBox1.ItemsSource = list;   

其中 list 是 List 集合。所有这些都非常有效 - 硬编码数据按预期显示在屏幕上。但是,当我尝试动态更新信息时,它悄然失败。如果我将一个操作绑定到运行此代码的按钮:

        list.Add(new NoteToSelf { Transcription = "FFUUUUUUUU", Duration = "LISTBOX, Y U NO UPDATE?" });
        listBox1.ItemsSource = list;

我希望它向集合中添加一个新元素,重新分配 DataTemplate 的源,然后使用新数据更新屏幕。然而事实并非如此。我该怎么做呢?

显然,这是 POC 代码,只是达到目的的一种手段,我现在只是希望获得动态更新的元素。另外,我很可能以错误的方式处理这个问题,如果有更好的方法从模板动态添加屏幕上的元素,那么听到它们将会非常有帮助。

谢谢大家。

Hey all, I'm writing a windows phone application in silverlight, and I'm trying to dynamically update the source of a DataTemplate I have in place. Here's the xaml that I have:

    <ListBox Grid.Row="1" Height="607" HorizontalAlignment="Left" Name="listBox1" VerticalAlignment="Top" Width="480">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Height="132">
                    <StackPanel Width="370">
                        <TextBlock Text="{Binding Transcription}" Foreground="#FFC8AB14" FontSize="28" />
                        <TextBlock Text="{Binding Duration}" TextWrapping="Wrap" FontSize="24" />
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

In the C# behind it, I pass the data along in the main init function like so:

        list.Add(new NoteToSelf { Transcription = "oh hi", Duration = "9001 seconds" });
        list.Add(new NoteToSelf { Transcription = "fgsfds", Duration = "$Texas seconds" });
        listBox1.ItemsSource = list;   

Where list is a List collection. All this works great - the hardcoded data is displayed as expected on screen. However, when I try to dynamically update the information, it silently fails. If I bind an action to a button that runs this code:

        list.Add(new NoteToSelf { Transcription = "FFUUUUUUUU", Duration = "LISTBOX, Y U NO UPDATE?" });
        listBox1.ItemsSource = list;

I would expect it to add a new element to the collection, reassign the source for the DataTemplate, then update the screen with the new data. This however is not the case. How would I go about doing this?

Obviously this is POC code that is simply a means to an end, I'm just looking to get elements to dynamically update at the moment. Also, I may very well be approaching this in the wrong way, if there is a better way to dynamically add elements on the screen from a template it would be immensely helpful to hear them.

Thanks all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁虫ゝ 2024-11-13 19:09:23

您没有说明列表是什么类型,但我将假设它是一个普通的列表<>。为了更新 UI,它需要有某种通知机制。该框架通过 INotifyPropertyChanged 支持这一点。您可以在数据绑定类上实现 INotifyPropertyChanged,并在添加时手动更新,或使用实现它的容器。最简单的方法是将您的 List<> 替换为与 ObservableCollection<>。

    var list = new ObservableCollection<NoteToSelf>();
    list.Add(new NoteToSelf { Transcription = "oh hi", Duration = "9001 seconds" });
    list.Add(new NoteToSelf { Transcription = "fgsfds", Duration = "$Texas seconds" });
    listBox1.ItemsSource = list;

You didn't state what type list was, but I'm going to assume its a plain List<>. In order to update the UI, it needs to have some sort of notification mechanism. The framework supports this through INotifyPropertyChanged. You can implement INotifyPropertyChanged on your databound class, and manually update as you add, or use a container that implements it. The easiest approach is to replace your List<> with an ObservableCollection<>.

    var list = new ObservableCollection<NoteToSelf>();
    list.Add(new NoteToSelf { Transcription = "oh hi", Duration = "9001 seconds" });
    list.Add(new NoteToSelf { Transcription = "fgsfds", Duration = "$Texas seconds" });
    listBox1.ItemsSource = list;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文