简体   繁体   中英

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. 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. Again so far so good. Now I have a method called

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) . If I can do so it will be easy to inject ItemsinGroup results to its related item in the list box loop. Can I do that by placing my method in 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 ?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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