Click here to Skip to main content
15,913,685 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm making my first steps in WPF. I would like to display the data of a childtable in a listbox.(parent-child related datatables) That part of the application is quite easy (even for me, as a beginner) But where things get really too complicated for me is when I try to display a nurmber just before the displayed listbox items. So let's say that if for a certain parent datarow there are five child-datarows, then the items in the listbox (childdata) should be numbered from 1 to 5. I thought of two textboxes in a datatemplate for the listbox. One for the number, and the second one for the item itself.
One possible way could be to work with a valueconverter in the binding of the "numberTextbox". But unfortunately there are only very few codeexamples of valueconverters with rownumbers in teh web. And the few examples I could find were coded in C#... (I feel more at ease with visual basic)
Could anybody help me with that please?

Thanks,
luccingolo
Posted

I do this by creating a custom ListBox style with an adjusted ItemContainerStyle.

<Style x:Key="FMListBox" TargetType="ListBox">
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="ListBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <Grid x:Name="PART_ItemGrid" >
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <TextBlock x:Name="PART_ItemNumber" Grid.Column="0" 
                                           Text={Binding Path=ItemNumber}" Margin="5" />
                                <TextBlock x:Name="PART_ItemText" Grid.Column="1" 
                                           Text="{Binding Path=Text}" 
                                           Margin="5" TextWrapping="Wrap" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>


Your collection object will have to have the two bound properties ItemNumber and Text to make the code above work
 
Share this answer
 
v2
Comments
Wendelius 6-Apr-11 15:42pm    
This was neat. My 5
Hi John,
thank you for your really fast answer.
But for me the difficult part of the coding is more the data-access than the xaml-part of the code.
In my case I have two related tables. As I already mentioned, the "numbered listbox" should be bound to the childtable. In the actual version of my WPF-project I have a "ViewModel-Class" with a Master- and a ChildView-property, both of ICollectionView-Type.
In your "scenario", with the "ItemNumber" and the "Text"-property. What would the "DataAccess-Class" look like. Because when the current Masterdatarow changes, the related childdatarows (with the ItemNumber and Text-properties) would have to change too. How would you do that, knowing that the Data is loaded from a SQL-Database, and is "accessed" through an Entity-Model in the application.

Thank you,
luccingolo
 
Share this answer
 
Comments
#realJSOP 6-Apr-11 15:22pm    
I'm afraid I can't help you with that because I don't (intentionally) do MVVM, MVC, or any of the other MV-paradigms. If you need to bind two tables to the same list, I think your best bet would be to create an ObservableCollection that contains just the data that you want to show in the list, along with references to the two table rows that provide the data.
Not sure if I understood the question correctly, but if you have two separate datatables you want to combine, perharps using LINQ would be an option. Something like this (pseudo):
int counter = 0;
var list = from item1 in datatable1.AsEnumerable()
           join item2 in datatable2.AsEnumerable()
           on item1.Fields<int>("ParentPK") equals item2.Fields<int>("ChildFK")
           orderby ...
           select new {
              Orderer = counter++,
              Field1 = item1.Fields<string>("Something"),
              Field2 = item2.Fields&lt;string&gt;("Else"),</int></int>
, ...
}
 
Share this answer
 
Comments
luccingolo 7-Apr-11 18:21pm    
Hi Mika, thank you for joining the discussion. But I'm not trying to combine two separate datatables. I am only trying to display the data of a related childdatatable in a ListBoxelement. I would like to display the listboxitem's number in front of the item itself. So, for example, if there are five items in the listbox, then they should be numbered from one to five. If the same listbox contains ten items, then these items should be numbered from one to ten...and so on. I use a datatemplate with two textboxes to diplay the data in the listbox. The first textbox for the number of the item, and the second textbox for the item itself.
The first problem is to find out how many items the listbox will be composed of every time the current related datarow of the parenttable changes. And then the first textbox of the listbox's datatemplate will have to be bound to the items number.(from 1 to listbox.items.count)
In my project I dont get the data right from the datatables but I "load" it in a data-entity model. And this entity-model is the "source" for my ViewModel-Class where a public ChildView-property(and a public parentView-property as well)of ICollectionView-Type keeps the updated childdatarows. At this time I have bound the textbox of the listbox's datatemplate to this public childview of my viewmodel-class. And now I hope that somebody can show me how to bind the other textbox of this datatemplate, to get a number for every listbox-item.
So I hope that now it's a little bit easier for you to understand what I'm trying to do here.
Thanks,
luccingolo

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900