WPF 将单个文本框绑定到集合对象或数组中的元素

发布于 2024-09-29 17:29:48 字数 487 浏览 0 评论 0原文

我需要将 textblock.text 属性绑定到可观察集合中的单个元素或数组元素,并使用 INotifyPropertyChangedINotifyCollectionChanged< 更新文本/code>,以最好的为准。

大多数示例描述了将列表框或其他列表视图绑定到整个集合的方法,但我的应用程序需要根据数组的一个或多个元素的更改通知来更新屏幕上的多个文本块。

textblock1.Text = MyArray(0)...
textblock2.Text = MyArray(1)...
textblock3.Text = MyArray(2)...
textblock4.Text = MyArray(3)...
etc...

是否可以将单个文本块绑定到单个数组元素?

如果任何分配的元素发生变化,是否可以获得正确类型的通知,该类型将更新一个或多个文本块?

I need to bind a textblock.text property to a single element in an observable collection, or array element, and have the text update using the INotifyPropertyChanged or INotifyCollectionChanged, whichever is best.

Most examples describe ways to bind listboxes or other list views to entire collections, but my application needs to update several textblocks on a screen depending on notification of a change in one or more elements of an array.

textblock1.Text = MyArray(0)...
textblock2.Text = MyArray(1)...
textblock3.Text = MyArray(2)...
textblock4.Text = MyArray(3)...
etc...

Is it possible to bind a single textblock to a single array element?

Is it possible to get notification of the proper type that will update one or more of the textblocks if any assigned element changes?

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

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

发布评论

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

评论(1

睫毛溺水了 2024-10-06 17:29:48

在 WPF 中,一切皆有可能,无论是哪种方式(或者通常是两种方式,还有更多)。

首先是简单的部分 - 如果您已经在数组中的对象上正确实现了 INotifyPropertyChanged,则绑定应该正确更新。 INotifyCollectionChanged 会通知您集合中的元素是否已更改(即添加/删除)。

听起来您正在尝试更新未知数量(甚至是已知数量,这并不重要)的 TextBlock。可能,最好的方法是将某种 ItemsControl(ListBox 就是其中之一)与 ItemsTemplate 和可选的 ItemsPanel 结合使用。如果集合的定义发生更改,这将是最简单的维护方法。

例如,下面是一个 ItemsControl 示例。

<ItemsControl x:Name="itemsExample"
              ItemsSource="{Binding MyCollection}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel HorizontalAlignment="Stretch" IsItemsHost="True" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding SomeStringProperty}" Grid.Column="0" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

但是,如果您确实想要绑定单个 TextBlock,可以通过实现 IValueConverter.然后,您将每个 TextBlock 绑定到该集合,并使用具有适当索引的 ConverterParameter。然后转换器将仅返回该索引处的字符串值。

<TextBlock Text="{Binding MyCollection,
                          Converter={StaticResource myObjectConverter},
                          ConverterParameter=0}" />

如果您使用 MVVM,另一种可能性是为数组的每个元素提供属性,并绑定到这些属性。但是,如果您这样做,我首先会质疑是否需要该数组。

All things are possible in WPF, one way or the other (or, usually, both, plus a bunch more).

The easy part first - if you've properly implemented INotifyPropertyChanged on the object in your array, bindings should update properly. INotifyCollectionChanged notifies you if elements in a collection have changed (i.e. been added/deleted).

It sounds like you are trying to update an unknown number (or even a known number, it doesn't really matter) of TextBlocks. Likely, the best way to do that is to use some kind of ItemsControl (ListBox is one) with an ItemsTemplate, and optionally the ItemsPanel. This will be the easiest way to maintain, in case the definition of the collection changes.

For instance, here is one ItemsControl example.

<ItemsControl x:Name="itemsExample"
              ItemsSource="{Binding MyCollection}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel HorizontalAlignment="Stretch" IsItemsHost="True" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding SomeStringProperty}" Grid.Column="0" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

If, however, you really do want to bind individual TextBlocks, one way you can do it by implementing IValueConverter. You would then bind each of your TextBlocks to the collection, and use a ConverterParameter with the appropriate index. The converter would then just return the value of a string at that index.

<TextBlock Text="{Binding MyCollection,
                          Converter={StaticResource myObjectConverter},
                          ConverterParameter=0}" />

If you are using MVVM, another possibility is to have properties for each of the elements of your array, and bind to those properties. However, if you are doing that, I would question the need for the array in the first place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文