J T

J T

  • NA
  • 16
  • 24.7k

Difficult to Create an automatical sorting in listview (WPF)

Mar 17 2011 7:03 AM
Goal:
Latest added data should be on the first line of in listview. The sorting part is based on column date.

Problem:
Dont't know how to make a automatical sorting in the listview after adding a data.

Please remember that I don't use syntax itemsource, List<XXXXXX> and Binding source.

public partial class FlightForm : Window
{

   
public delegate void TakeOffHandler(object source, TakeOffEventArgs e);
   
public delegate void ChangeHandler(object source, ChangeRouteEventArgs e);

   
public event TakeOffHandler TakeOffEvent;
   
public event ChangeHandler ChangeEvent;


   
public FlightForm()
   
{
       
InitializeComponent();

       
Title = "Flight ";

        cmbStatus
.Visibility = Visibility.Hidden;
        btnLand
.Visibility = Visibility.Hidden;
   
}



   
private void btnStart_Click(object sender, RoutedEventArgs e)
   
{

        cmbStatus
.Visibility = Visibility.Visible;
        btnLand
.Visibility = Visibility.Visible;
        btnStart
.Visibility = Visibility.Hidden;

       
TakeOffEvent(this, new TakeOffEventArgs("a", "b", DateTime.Now.ToString()));
       
ChangeEvent(this, new ChangeRouteEventArgs("aa", "bb", "cc"));


   
}




}

------------------------------------------

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class ControlTower : Window
{
   
public ControlTower()
   
{
       
InitializeComponent();
   
}

   
private FlightForm myFlightForm;


   
private void btnSendNextAirplane_Click(object sender, RoutedEventArgs e)
   
{
        myFlightForm
= new FlightForm();

        myFlightForm
.TakeOffEvent += new FlightForm.TakeOffHandler(PrintOutTakeOff);
        myFlightForm
.ChangeEvent += new FlightForm.ChangeHandler(PrintOutChange);
        myFlightForm
.Show();

   
}




   
public void PrintOutTakeOff(object source, TakeOffEventArgs e)
   
{

        lstDisplay
.Items.Add(new { FlightCode = e.FlightCode, Status = e.Status, Time = e.Time });

   
}


   
public void PrintOutChange(object source, ChangeRouteEventArgs e)
   
{
       
string test = e.FlightCode + e.Status + e.Time;

       
MessageBox.Show(test);
   
}




}

-------------------------------------------

<Window x:Class="Assignment3.ControlTower"
        xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns
:x="http://schemas.microsoft.com/winfx/2006/xaml"
       
Title="Control Tower" Height="326" Width="420">
   
<Grid Height="282">
       
<Button Content="Send next Airplane to Runway" Height="23" HorizontalAlignment="Left" Margin="225,250,0,0" Name="btnSendNextAirplane" VerticalAlignment="Top" Width="163" Click="btnSendNextAirplane_Click" />
       
<TextBox Height="23" HorizontalAlignment="Left" Margin="81,250,0,0" Name="txtFlightCode" VerticalAlignment="Top" Width="120" />
       
<Label Content="Next flight:" Height="28" HorizontalAlignment="Left" Margin="6,250,0,0" Name="label1" VerticalAlignment="Top" />
       
<ListView Height="244" HorizontalAlignment="Left" Margin="12,0,0,0" Name="lstDisplay" VerticalAlignment="Top" Width="372">
           
<ListView.View>
               
<GridView>
                   
<GridViewColumn Header="Flight Code" Width="70" DisplayMemberBinding="{Binding FlightCode}" />
                   
<GridViewColumn Header="Status" Width="160" DisplayMemberBinding="{Binding Status}" />
                   
<GridViewColumn Header="Time" Width="120" DisplayMemberBinding="{Binding Time}" />
               
</GridView>
           
</ListView.View>
       
</ListView>
   
</Grid>
</Window>

------------------------------------

<Window x:Class="Assignment3.FlightForm"
        xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns
:x="http://schemas.microsoft.com/winfx/2006/xaml"
       
Title="FlightForm" Height="305" Width="352">
   
<Grid>
       
<Button Content="Start" Height="23" HorizontalAlignment="Left" Margin="0,216,0,0" Name="btnStart" VerticalAlignment="Top" Width="75" Click="btnStart_Click" />
       
<ComboBox Height="23" HorizontalAlignment="Left" Margin="81,216,0,0" Name="cmbStatus" VerticalAlignment="Top" Width="120" Visibility="Visible" />
       
<Button Content="Land" Height="23" HorizontalAlignment="Left" Margin="217,216,0,0" Name="btnLand" VerticalAlignment="Top" Width="75" />
   
</Grid>
</Window>





Answers (1)