gpt4 book ai didi

c# - WPF 组合框和 ListView 子项问题

转载 作者:行者123 更新时间:2023-11-30 21:13:47 24 4
gpt4 key购买 nike

我需要帮助来完成一项给我带来困难的任务。它使用 WPF 和 C#。

  1. 如何每分钟更新一个子项目?

  2. 如何提取所选组合框的字符串并将其添加到 ListView 中的子项?

  3. 有时我必须使用任务管理器来关闭我的应用程序,我不知道为什么?

这是我目前拥有的部分代码以及它应该是什么样子的图像。

Application

主界面

    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();
}
}
}

飞行图形用户界面

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;
}
}

}
}

更改路由类,这是我在从组合框中获取所选项目并进入 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();
}
}
}

非常感谢您的帮助! =)

编辑:

嗯,我猜你想让我给你看我的 xaml 代码?

<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>

航类 xaml

<Window x:Class="Assignment_3.Flight"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.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>

那我现在该怎么办? =/我不擅长这个。

最佳答案

你的代码真的很乱,写得不好。作为 WPF 应用程序,您还应该考虑 XAML 代码。

你要做的事情很简单,但是使用“旧式编程”将是一场噩梦,有很多麻烦和不稳定。我强烈建议使用 MVVM 方法,乍一看可能很复杂,但它会产生很好的结果。一旦你学会了,你就再也不会离开了。

关于c# - WPF 组合框和 ListView 子项问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6674439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com