Sorting a listbox by columns

Abe

Member
Joined
Mar 16, 2017
Messages
20
Programming Experience
Beginner
I have a list box that has some inventory in it. What I need to do is when I press Qty, it sorts the entire table but also the row as 1 unit. So in the image example, if I have the Qty 2 for the first row and qty 3 for the 2nd row it will put The entire row, (Padded Leather) on top and (Shield) below it.

Here is the code I have now and it is not sorting correctly.
C#:
        private void Label_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            tab_armor.Items.SortDescriptions.Add(
            new System.ComponentModel.SortDescription("qty",
            System.ComponentModel.ListSortDirection.Ascending));
        }
1234.PNG
 
Try .SortDescriptions.Clear() before adding.
 
Try .SortDescriptions.Clear() before adding.

If you do that then it will presumably sort by just that column without regard for any previous sort. That may be what is wanted, but it may also be that you want to keep the previous sort as a secondary. In that case, I think that you would not call Clear but call Insert instead of Add, so the new sort was done first instead of last. You also wouldn't want to allow the same column to be sorted on twice, because that could lead to a really long list of SortDescriptions, so you would have to check whether the new column was already being sorted on and remove that too.
private void SortArmor(string columnName)
{
    var sortDescription = tab_armor.Items.SortDescriptions.SingleOrDefault(sd => sd.PropertyName == columnName);

    if (sortDescription.PropertyName == null)
    {
        // The list was NOT already sorted by this column.
        sortDescription.PropertyName = columnName;
        sortDescription.Direction=ListSortDirection.Ascending;
    }
    else
    {
        // The list was already sorted by this column.
        tab_armor.Items.SortDescriptions.Remove(sortDescription);
    }

    tab_armor.Items.SortDescriptions.Insert(0, sortDescription);
}

I haven't tested that but it seems like it should work.
 
Thank you for getting back to me. I added in the line and it is still doing the same thing. Thinking about it, It is probably because I do not have set Columns? All the text you see is just labels I placed in the ListBox. I am not sure how to declare the top row as columns. Assuming that is why it is not working?

Here is the XAML of the List box

C#:
 <ListBox x:Name="tab_armor" Width="620"  ScrollViewer.HorizontalScrollBarVisibility="Disabled" Margin="13,172,10.2,38.8" Background="{x:Null}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Left" ScrollViewer.VerticalScrollBarVisibility="Visible" BorderBrush="{x:Null}" AutomationProperties.IsColumnHeader="True">
                        <ListBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <UniformGrid Columns="8" />
                            </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>
//////////This is the manually added labels, which I need to sort by.
                        <Label Content="Sort" HorizontalAlignment="Center" VerticalAlignment="Top" Width="74" FontFamily="UnZialish" FontSize="12" FontWeight="Bold" Foreground="#FF07BCFF" HorizontalContentAlignment="Center"/>
                        <Label Content="Name" HorizontalAlignment="Center" VerticalAlignment="Top" Width="74" FontFamily="UnZialish" FontSize="12" FontWeight="Bold" Foreground="#FFCAA201" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                        <Label Content="AC" HorizontalAlignment="Center" VerticalAlignment="Top" Width="74" FontFamily="UnZialish" FontSize="12" FontWeight="Bold" Foreground="#FFCAA201" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                        <Label Content="Stealth" HorizontalAlignment="Center" VerticalAlignment="Top" Width="74" FontFamily="UnZialish" FontSize="12" FontWeight="Bold" Foreground="#FFCAA201" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                        <Label Content="Cost" HorizontalAlignment="Center" VerticalAlignment="Top" Width="74" FontFamily="UnZialish" FontSize="12" FontWeight="Bold" Foreground="#FFCAA201" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                        <Label Content="Weight" HorizontalAlignment="Center" VerticalAlignment="Top" Width="74" FontFamily="UnZialish" FontSize="12" FontWeight="Bold" Foreground="#FFCAA201" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                        <Label Content="$" VerticalAlignment="Top" Width="74" FontFamily="Baskerville Old Face" FontSize="16" FontWeight="Bold" Foreground="#FFFFE500" HorizontalAlignment="Center" HorizontalContentAlignment="Center"/>
                        <Label x:Name="qty" Content="Qty" HorizontalAlignment="Center" VerticalAlignment="Top" Width="51" FontFamily="UnZialish" FontSize="12" FontWeight="Bold" Foreground="#FFFFE500" HorizontalContentAlignment="Center" Grid.IsSharedSizeScope="True" AutomationProperties.IsColumnHeader="True" AutomationProperties.Name="qty" MouseUp="qty_MouseUp"/>

//////////This is an example of 1 of the rows.
                        <Image x:Name="ImageShield" HorizontalAlignment="Center" Height="63" VerticalAlignment="Top" Width="74" Source="Images/i_shield.png" Stretch="UniformToFill" ToolTip="Right Click to + Left Click to -" MouseLeftButtonUp="ImageShield_MouseLeftButtonUp" MouseRightButtonUp="ImageShield_MouseRightButtonUp"/>
                        <Label Content="Shield" HorizontalAlignment="Center" VerticalAlignment="Top" Width="74" FontFamily="UnZialish" FontSize="10" FontWeight="Bold" Foreground="#FFADA5A5" Height="47" VerticalContentAlignment="Center" FontStretch="Condensed" HorizontalContentAlignment="Center"/>
                        <Label Content="2" HorizontalAlignment="Center" VerticalAlignment="Top" Width="74" FontFamily="UnZialish" FontSize="10" FontWeight="Normal" Foreground="#FFADA5A5" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
                        <Label Content="N/A" HorizontalAlignment="Center" VerticalAlignment="Top" Width="74" FontFamily="UnZialish" FontSize="10" FontWeight="Normal" Foreground="#FFADA5A5" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
                        <Label Content="10gp" HorizontalAlignment="Center" VerticalAlignment="Top" Width="74" FontFamily="UnZialish" FontSize="10" FontWeight="Normal" Foreground="#FFADA5A5" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
                        <Label Content="6lbs" HorizontalAlignment="Center" VerticalAlignment="Top" Width="74" FontFamily="UnZialish" FontSize="10" FontWeight="Normal" Foreground="#FFADA5A5" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
                        <Label Content="$" VerticalAlignment="Top" Width="74" FontFamily="Baskerville Old Face" FontSize="16" FontWeight="Bold" Foreground="#FFFFE500" HorizontalAlignment="Center" HorizontalContentAlignment="Center" MouseDown="Label_MouseDown"/>
                        <TextBox x:Name="qty1" Height="23" TextWrapping="Wrap" Text="0" Width="52" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="#FF4DB425" MaxLength="7" FontSize="12" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
 
Back
Top Bottom