3

I have been battling this issue for quite a while and I need some expert help to get the result I want. Here is the result of what my listview with gridviewcolumn looks like by binding the source as lstPerson and it's DataMember to Gender and Name.

Person class:

private int ID { get; set; }
private string Gender { get; set; }
private string FName { get; set; }

List<Person> lstPerson = GetPersonInformation();
//select Gender, FirstName From Person Order By Gender

ListView Result:

John
Mike
Gabriel
Kevin
Peter
Stacy
Jen
Lily
Lisa
Vivian

The above is not what I want to display. If I wanted the above, it would be easy to do. The below result is what I want to achieve instead ... for this example, max of 4 columns and max of 3 rows ... every 3 rows, create a column with 3 rows until 4 columns is reach.

Male     Gabriel     Female     Lily
John     Kevin       Stacy      Lisa
Mike     Peter       Jen        Vivian

Does anyone know how I can achieve this either through XAML or code behind?

4
  • May you please provide what does GetPersonInformation() actually do? :) Dec 7, 2012 at 0:20
  • @Picrofo EGY -- as posted above ... select Gender, Name From Person Order By Gender ... just getting the fname(first) and gender from my person table and ordering them by gender. Dec 7, 2012 at 0:23
  • It's not clear from your "what I want" output what you actually want to happen in terms of layout. If there are only two males, should there just be one column under 'Male', or should there be two rows and two columns with an empty cell in the lower-right? Is it important that 'Male' always be on the left? If a third 'Unknown' gender appears, where does that go? Can the user click on 'Female' and have it be selected? What happens when 'Gabriel' is selected and the user hits the right arrow key? Would a 6th female show up to the right in a 5th column, or below the existing females? Dec 7, 2012 at 1:15
  • @RyanCavanaugh -- so the total max of columns would be 4. if the row is equal to 3 in the first column, i would like it to continue to create a second column with another 3 row, create next column with three row, until the column is equal to 4. just an example. But to also be more specific, if there is only 1 male, there would just be one column with two row(male, john). does this make sense? Dec 7, 2012 at 1:42

2 Answers 2

0

You can try to play with ItemsPanelTemplate:

 <ListView>
     <ListView.ItemsPanel>
         <ItemsPanelTemplate>
             <WrapPanel Width="200" />
         </ItemsPanelTemplate>
     </ListView.ItemsPanel>
     <ListView.ItemTemplate>
         <DataTemplate>
             <TextBlock Margin="5 0" Text="{Binding FName}" />
         </DataTemplate>
     </ListView.ItemTemplate>
 </ListView>

If you need exactly 4 columns even if the names are very long you can use UniformGrid instead of WrapPanel:

<ItemsPanelTemplate>
     <UniformGrid Columns="4" />
</ItemsPanelTemplate>
4
  • thanks that put in on the right track but how would I add like the categories (Male, Female) for it? Dec 7, 2012 at 13:14
  • It depends on what you want. Just edit ListView.ItemTemplate and add there anything you like. For example, you can add another TextBlock for category there.
    – icebat
    Dec 7, 2012 at 13:53
  • If you look at what I did in your post, I'm getting gender for each item. I only want it to show once per my result I've posted that I want to see. Dec 7, 2012 at 14:48
  • If you need items separated by gender you better use two ListViews. Make two List<Person> and bind to them (you can make them with LINQ: GetPersonInformation().Where(p => p.Gender=="Female") or so.
    – icebat
    Dec 10, 2012 at 9:29
0

Your need rightsorted list - and thats all!

<ListBox x:Name="lbPeoples" ScrollViewer.VerticalalScrollBarVisibility="Disabled" ItemsSource="{Binding Path=Peoples}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Vertical" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

Where: ListBox Height = 3*Item_Height, ListBox Width = 4*Item_Width

Peoples[0] = Male, Peoples[6] = Femail

Not good way - but simple!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.