如何将两个不同的数据集合绑定到 2 个不同的 PanoramaItem 列表框中?

发布于 2024-12-27 19:59:44 字数 682 浏览 3 评论 0原文

我有 AItems 和 BItems,想要绑定到 PanoramaItem 中的 AListBox 和另一个 PanoramaItem 中的 BListBox。

我在每个 ListBox 中指定了 ItemsSource

<controls:PanoramaItem>
    <ListBox ItemsSource="{Binding AItems}" >
      ...
    </ListBox>
</controls:PanoramaItem>
<controls:PanoramaItem 
    <ListBox ItemsSource="{Binding BItems}" >
      ...
    </ListBox>
</controls:PanoramaItem>

如何从代码隐藏将数据绑定到每个 ListBox?

我有构造函数方法

public MainPage()
{
    InitializeComponent();
}

和页面加载方法

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
}

谢谢。

I have AItems and BItems, want to bind to AListBox in a PanoramaItem and BListBox in another PanoramamItem.

I specified ItemsSource in each ListBox

<controls:PanoramaItem>
    <ListBox ItemsSource="{Binding AItems}" >
      ...
    </ListBox>
</controls:PanoramaItem>
<controls:PanoramaItem 
    <ListBox ItemsSource="{Binding BItems}" >
      ...
    </ListBox>
</controls:PanoramaItem>

How do I bind the data to each ListBox from code behind?

I have the Constructor method

public MainPage()
{
    InitializeComponent();
}

and page load method

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
}

Thank you.

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

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

发布评论

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

评论(1

如果没有 2025-01-03 19:59:44

要将集合绑定到代码隐藏中的控件:

1) 指定列表框名称
2) 在后面的代码中,设置.ItemsSource =;

(您应该确保您的集合是 ObservableCollection以确保如果集合发生更改,这会反映在视图中。)

例如...

<controls:PanoramaItem>
    <ListBox Name="AItemBox" >
      ...
    </ListBox>
</controls:PanoramaItem>
<controls:PanoramaItem 
    <ListBox Name="BItemBox" >
      ...
    </ListBox>
</controls:PanoramaItem>

然后

private void BindMyControls()
{
    AItemBox.ItemSource = AItems;
    BItemBox.ItemSource = BItems;
}

您可以从以下位置调用 BindMyControls()最合适的地方,很可能是在收藏品被填充之后。

To bind the collections to controls in code-behind :

1) Give the listboxes names
2) in code behind, set <listboxName>.ItemsSource = <collection>

(You should make sure your collections are ObservableCollection<myType> to ensure that if the collection changes, this is reflected in the view.)

for example...

<controls:PanoramaItem>
    <ListBox Name="AItemBox" >
      ...
    </ListBox>
</controls:PanoramaItem>
<controls:PanoramaItem 
    <ListBox Name="BItemBox" >
      ...
    </ListBox>
</controls:PanoramaItem>

and then

private void BindMyControls()
{
    AItemBox.ItemSource = AItems;
    BItemBox.ItemSource = BItems;
}

Then you can call BindMyControls() from the most appropriate place, most likely once the collections have been populated.

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