开发者

WPF combobox and listview subitem issues

开发者 https://www.devze.com 2023-03-19 17:01 出处:网络
I need help with an assignment that is giving me a hard time. It\'s using WPF and C#. How do I update a subitem every minute?

I need help with an assignment that is giving me a hard time. It's using WPF and C#.

  1. How do I update a subitem every minute?

  2. How to excract the string of the selected combobox and add that to a subitem in a listview?

  3. I have to use the task manager to close my application sometimes, I don't know why?

Here is part of the code I have so far and an image of how it's kinda supposed to look like.

WPF combobox and listview subitem issues

Main GUI

    namespace Assignment_3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public int time=0;
    public MainWindow()
    {
        InitializeComponent();
        UpdateGUI();
    }


    public void UpdateGUI()
    {

        DispatcherTimer dispatchTimer = new DispatcherTimer();
        dispatchTimer.Tick += new EventHandler(dispatchTimer_Tick);
        dispatchTimer.Interval = new TimeSpan(0,0,1);
        dispatchTimer.Start();
    }

    public void dispatchTimer_Tick(object sender, EventArgs e)
    {

        //    lvFlights.Items.Add(DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Second.ToString());
       // lvFlights.Items.Add(new { FlightCode = txtFlightCode.Text, Status = "sent to runaway", Time = DateTime.Now.Millisecond.ToString() });// lvFlights.Items.Count });
        time ++;

       // txtFlightCode.Text = (Convert.ToString(time));
        CommandManager.InvalidateRequerySuggested();
        lvFlights.Items.MoveCurrentToLast();
        lvFlights.SelectedItem = lvFlights.Items.CurrentItem;
        lvFlights.ScrollIntoView(lvFlights.Items.CurrentItem);

    }

    public void button1_Click(object sender, RoutedEventArgs e)
    {

        Flight flight = new Flight();
        lvFlights.Items.Add(new { FlightCode = txtFlightCode.Text, Status = "    sent to runway", Time = time +" minutes ago"});
        Sort("Time", ListSortDirection.Descending);

        flight.Title = "Flight " + txtFlightCode.Text;

        flight.TakeOffEvent += new Flight.TakeOffHandler(PrintOutTakeOff);
        flight.ChangeEvent += new Flight.ChangeHandler(PrintOutChange);
        flight.LandEvent+=new Flight.LandHandler(PrintOutLand);

        try
        {
            flight.Show();
        }
        catch { MessageBox.Show("damn, u can't program"); }
    }

    public void PrintOutTakeOff(object source, TakeOffEventArgs e)
    {
        lvFlights.Items.Add(new { FlightCode = txtFlightCode.Text, Status = e.Status, Time = e.Time });
        Sort("Time", ListSortDirection.Descending);
    }
    public void PrintOutChange(object source, ChangeRouteEventArgs e)
    {
        lvFlights.Items.Add(new { FlightCode = txtFlightCode.Text, Status = e.Status, Time = e.Time });
        Sort("Time", ListSortDirection.Descending);
    }

    public void PrintOutLand(object source, LandEventArgs e)
    {
        lvFlights.Items.Add(new { FlightCode = txtFlightCode.Text, Status = e.Status, Time = e.Time });
        Sort("Time", ListSortDirection.Descending);
    }

    private void Sort(string sortBy,ListSortDirection direction)
    {
        //The MSDNversionpass ItemsSource property
        ICollectionView dataView=CollectionViewSource.GetDefaultView(lvFlights.Items);

        dataView.SortDescriptions.Clear();
        SortDescription sd =new SortDescription(sortBy, direction);
        dataView.SortDescriptions.Add(sd);
        dataView.Refresh();
    }
}
}

Flight GUI

namespace Assignment_3
{
/// <summary>
/// Interaction logic for Flight.xaml
/// </summary>
public partial class Flight : Window
{
  public   delegate void TakeOffHandler(object source, TakeOffEventArgs e);
   public  delegate void ChangeHandler(object source, ChangeRouteEventArgs e);
   public delegate void LandHandler(object source, LandEventArgs e);

   public  event TakeOffHandler TakeOffEvent;
   public  event ChangeHandler ChangeEvent;
   public event LandHandler LandEvent;



    public Flight()
    {
        InitializeComponent();

        cmbStatus.IsEnabled = false;
        btnLand.IsEnabled = false;
    //    image1.Source = global::Assignment_3.Properties.Resources.airplane2;

    }

    private void btnStart_Click(object sender, RoutedEventArgs e)
    {
        cmbStatus.IsEnabled = true;
        btnLand.IsEnabled = true;
        btnStart.IsEnabled = false;

        try
        {
            TakeOffEvent(this, new TakeOffEventArgs("{0}", "{1}",     DateTime.Now.ToString()));

        }
        catch { MessageBox.Show("lern 2 progrm"); }
     //   MainWindow main = new MainWindow();
   //   main.lvFlights.Items.Add(this.Title + "    sent to runway" + "   1 minute ago");

    }



    private void btnLand_Click(object sender, RoutedEventArgs e)
    {
        LandEvent(this, new LandEventArgs("aa", "bb", "cc"));
    }

    public void cmbStatus_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        string route;  
      switch (cmbStatus.SelectedIndex)
        {
            case 0: route = "10 deg"; ChangeEvent(this, new ChangeRouteEventArgs("aa", "bb", "cc"));
                break;
            case 1: route = "30 deg"; ChangeEvent(this, new ChangeRouteEventArgs("aa", "bb", "cc"));
                break;
            case 2: route = "60 deg"; ChangeEvent(this, new ChangeRouteEventArgs("aa", "bb", "cc"));
                break;
            case 3: route = "90 deg"; ChangeEvent(this, new ChangeRouteEventArgs("aa", "bb", "cc"));
                break;
      }
    }

    }
}

Change Route class, here is where I'm having problems getting the selected item from the combobox and getting in the listview

namespace Assignment_3
{
public class ChangeRouteEventArgs
{
    public string FlightCode;
    public string Status;
    public string Time;
    Flight flight =new Flight();


    public ChangeRouteEventArgs(string FlightCode, string Satus, string Time)
    {
        // TODO: Complete member initialization
        switch (flight.cmbStatus.SelectedIndex)
        {
            case 0: this.Status = "    now heading "+"10 deg";
                break;
            case 1: this.Status = "    now heading " + "30 deg";
                break;
            case 2: this.Status = "    now heading " + "60 deg";
                break;
            case 3: this.Status = "    now heading " + "90 deg";
                break;

        }

        this.FlightCode = "dis reelee werkt";
        this.Status = "    now heading " + flight;
        this.Time = "   "+DateTime.Now.Minute.ToString();
    }
}
}

I would be extremely thankful for your help! =)

Edit:

Well I'm guessing you want me to show you my xaml codes?

<Window x:Class="Assignment_3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Control Tower" Height="276" Width="407" ResizeMode="NoResize" ShowInTaskbar="True" WindowStyle="ThreeDBorderWindow">
    <Grid>
        <ListView Height="170" HorizontalAlignment="Left" Margin="12,12,0,0" Name="lvFlights" VerticalAlignment="Top" Width="373">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Flight Code" Width="90" DisplayMemberBinding="{Binding FlightCode}"/>
                    <GridViewColumn Header="Status" Width="160" DisplayMemberBinding="{Binding Status}"/>
                    <GridViewColumn Header="Time" Width="120" DisplayMemberBinding="{Binding Time}"/>
                </GridView>
            </ListView.View>
        </ListView>
        <Label Content="Next Flight" Height="28" HorizontalAlignment="Left" Margin="12,197,0,0" Name="label1" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="86,200,0,0" Name="txtFlightCode" VerticalAlignment="Top" Width="88" Text="flight code" />
        <Button Content="Send Next Airplane to Runaway" Height="23" HorizontalAlignment="Right" Margin="0,199,12,0" Name="btnSend" VerticalAlignment="Top" Width="188" Click="button1_Click" />
    </Grid>
</Window>

Flight xaml

<Window x:Class="Assignment_3.Flight"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://sc开发者_如何学Chemas.microsoft.com/winfx/2006/xaml"
        Title="Flight" Height="244" Width="277" ResizeMode="NoResize" WindowStyle="ToolWindow">
    <Grid>
        <Image Height="159" HorizontalAlignment="Left" Margin="12,12,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="231" Source="file:///C:/Users/Hans/Documents/Visual%20Studio%202010/Projects/Assignment%203/Assignment%203/Resources/airplane2.jpg" />
        <Button Content="Start" Height="23" HorizontalAlignment="Left" Margin="12,177,0,0" Name="btnStart" VerticalAlignment="Top" Width="60" Click="btnStart_Click" />
        <Button Content="Land" Height="23" HorizontalAlignment="Right" Margin="0,177,12,0" Name="btnLand" VerticalAlignment="Top" Width="60" Click="btnLand_Click" />
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="78,177,0,0" Name="cmbStatus" VerticalAlignment="Top" Width="99"  SelectedIndex="-1" SelectionChanged="cmbStatus_SelectionChanged" SelectedItem="{Binding Item}">

            <ComboBoxItem Content="10 deg" />
            <ComboBoxItem Content="30 deg" />
            <ComboBoxItem Content="60 deg" />
            <ComboBoxItem Content="90 deg" />
        </ComboBox>
    </Grid>
</Window>

So what do I do now? =/ I'm bad at this.


Your code is really messy and bad written. Being a WPF application, you should consider the XAML code also.

What you are going to do is pretty simple, but by using a "old-style-programming" will be a nightmare, with lot of troubles and instability. I strongly suggest the MVVM approach, that might be complicated at first glance, but it yields excellent results. Once you'll learn, you never left anymore.


to get the SelectedItem from a ComboBox you can use simple Binding.

<ComboBox SelectedValue="{Binding Path=MySelectedItemProperty}"
          ItemsSource="{Binding Path=MyItemslistProperty}"/>

EDIT. if you use binding or some kind of mvvm, you can simply add new items to your listview if you to the following. i name the type of the listviewitem TypL

  • use a ObservableCollection of TypL and bind it to the Itemssource of your ListView
  • simply use the Add Methode to add new Items to your Collection

sample binding

<ListView ItemsSource="{Binding Path=MyObservableCollectionProperty}"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消