简体   繁体   English

WP7:基于数据库中的数据访问和更改ListBox DataTemplate内部的控件

[英]WP7 : access and change controls inside a ListBox DataTemplate based on the data from the database

I know this has been asked before but Im a bit confused ! 我知道这已经被问过了,但是我有点困惑! I'm writing my app with MVVM so far so good. 到目前为止,我正在用MVVM编写我的应用程序。 but now I need to know what is the best way to access my controls inside a data template in a listbox. 但是现在我需要知道在列表框中的数据模板中访问控件的最佳方法是什么。 I want to access them through the code behind and also be able to change them based on the other values from the database ! 我想通过后面的代码访问它们,并且还能够基于数据库中的其他值来更改它们!

Here is the view : 这是视图:

                <ListBox Margin="0,8,0,0"  toolkit:TiltEffect.IsTiltEnabled="True" x:Name="counterlist" ItemsSource="{Binding Groups}" HorizontalAlignment="Center" Tap="list_OnTap" 
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Background="Orange" Width="125" Height="125" Margin="6">
                        <TextBlock Name="name" Foreground="White" Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap"/>
 <TextBlock Name="items" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <toolkit:WrapPanel/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>

This list box is bound to the Groups Observable collection in my viewModel. 此列表框绑定到我的viewModel中的Groups Observable集合。 Again so far so good. 到目前为止,一切都很好。 Now I have a method called 现在我有一个方法叫做

public int ItemsinGroup(int gid) public int ItemsinGroup(int gid)

This method returns number of items in each group but its not simply based on this database and it also get some info from an external source so I cant simply make a query to add this also to the observable collection. 该方法返回每个组中的项目数,但它不仅基于该数据库,而且还从外部来源获取一些信息,因此我不能简单地通过查询将其也添加到可观察的集合中。

I just need to add this to each item in the list box so that it shows the associated item counts for each group. 我只需要将此添加到列表框中的每个项目,以便它显示每个组的关联项目计数。

I want to be able to change it in the code behind. 我希望能够在后面的代码中对其进行更改。 I mean I want to access each loop of data in code behind as well as XAML (Which we are already doing it through binding) . 我的意思是我想访问代码背后的每个数据循环以及XAML(我们已经通过绑定来实现)。 If I can do so it will be easy to inject ItemsinGroup results to its related item in the list box loop. 如果可以的话,很容易将ItemsinGroup结果注入到列表框循环中的相关项目中。 Can I do that by placing my method in viewModel ? 我可以通过将我的方法放在viewModel中来做到这一点吗? but what about the current item in the loop, how can I find out what is the current ID of each Group in the listbox loop ? 但是循环中的当前项目如何,我如何找出列表框循环中每个组的当前ID是什么?

I need to know what is the best way to do such things, what usually everyone does in these cases ! 我需要知道做这些事情的最佳方法是什么,在这种情况下每个人通常都会做什么!

Thanks a lot 非常感谢

Your desire to use MVVM and yet update individual items in a collection from code behind seem contradictory. 您想要使用MVVM并从背后的代码更新集合中的单个项目的愿望似乎矛盾。

This simplest solution is probably to get your collection of groups and before you bind them to the UI you loop through them and add the count to the objects in your collection. 最简单的解决方案可能是获取组的集合,然后在将它们绑定到UI之前,先遍历它们并将计数添加到集合中的对象。

Alternatively, if you want to bind the collection as quickly as possible and then update it. 或者,如果您想尽快绑定集合,然后更新它。 You could step through the collection once bound and as long as the objects in your collection implement INotifyPropertyChanged you could update them and have a second TextBlock in the ItemTemplate show this when set. 您可以在绑定后逐步浏览集合,只要集合中的对象实现INotifyPropertyChanged,您就可以更新它们,并在ItemTemplate中使用第二个TextBlock进行设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM