如何解决WPF C#:如何从ListView中的选定行获取ID,以通过事件处理程序打开唯一的窗口?
我用表“ activemeassurements”创建了一个mysql数据库。我在BatchesVm.cs
中运行了一个查询,该查询输出的行具有最新日期,并且对<ListView>
中的MainWindow.xaml.cs
具有唯一的批处理ID。我希望每当我双击所选项目时,便可以在另一个窗口中更详细地显示所选项目。绑定工作正常,并且已连接到正确的数据库。但是,我一直在努力寻找一种方法来检索ListView>中所选行的ID以在下一个Window中使用。这是我第一次使用wpf。
XAML:
<Window x:Class="P5_WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:P5_WPF" xmlns:local1="clr-namespace:P5_WPF.ViewModels"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="8*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<ListView ItemsSource="{Binding allBatches}" x:Name="batcheslist" Margin="72,100,59,63" Grid.ColumnSpan="2" SelectedItem="{Binding Path=BatchID}" >
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=batchid" Header="batch ID" />
<GridViewColumn DisplayMemberBinding="{Binding Path=Temp}" Header="Latest temp meassurements"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Humidity}" Header="Latest humidity meassurements" />
<GridViewColumn DisplayMemberBinding="{Binding Path=date}" Header="Date" Width="150"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new BatchesVm();
}
private void batchesList_MouseDoubleClick(object sender,MouseButtonEventArgs e)
{
//I want to open single batch window with the id from the selected item.
SingleBatch_Window single = new SingleBatch_Window();
single.Show();
}
}
我的BatchesVm.cs
C#文件,其中包含ListView的ItemsSource:
public class BatchesVm
{
public DataView allBatches { get; private set; }
public BatchesVm()
{
var CS = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
DataTable dt = new DataTable();
try
{
using (MySqlConnection connection = new MySqlConnection(CS))
{
string CmdString = "select batchid,Temp,Humidity,date from (select batchid,date,row_number() over(partition by batchid order by date desc) as rn from aktivemeassurements) t where t.rn = 1;";
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = new MySqlCommand(CmdString,connection);
adapter.Fill(dt);
}
allBatches = dt.DefaultView;
}
catch (Exception ex)
{
MessageBox.Show("Cannot establish connection");
MessageBox.Show(ex.Message);
}
}
}
-我如何:
- 使用事件处理程序
batchesList_MouseDoubleClick,
并获取 所选行的批处理标识符,并用它打开一个唯一的窗口?
================================================ ========================
解决方法
对于任何想知道相同解决方案的人:通过@Jdweng
的帮助,我弄清楚了如何从DataView allBatches
检索ID。
来自MainWindow.xaml.cs
private void batchesList_MouseDoubleClick(object sender,MouseButtonEventArgs e)
{
try
{
//get index of the selected row.
int itemindex = batcheslist.Items.IndexOf(batcheslist.SelectedItems[0]);
BatchesVm a = new BatchesVm();
//get id of selected row.
int batchId = a.allBatchIds[itemindex];
SingleBatch_Window single = new SingleBatch_Window(batchId);
single.Show();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("Please choose a Batch.");
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
ViewModel:
//Dataview of the selected sql table from query
allBatches = dt.DefaultView;
//converting DataView allBatches to a generic list.
allBatchIds = allBatches.ToTable().Rows.OfType<DataRow>()
.Select(dr => dr.Field<int>("BatchID")).ToList();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。