我在C#中创建了listview,并用SQL server中的数据填充它。但是,当我指定鼠标双击时,我不知道如何获取点击数据。请帮忙:
我的XAML:
<ListView Name="ListViewEmployeeDetails" ItemsSource="{Binding Path=Table}" Margin="0,0,0,67" MouseDoubleClick="ListViewEmployeeDetails_MouseDoubleClick">
<ListView.Background>
<LinearGradientBrush>
<GradientStop Color="white" Offset="0"/>
</LinearGradientBrush>
</ListView.Background>
<ListView.View>
<GridView>
<GridViewColumn Width="70" Header="Číslo bytu" DisplayMemberBinding="{Binding Path=cislo_Bytu}"/>
<GridViewColumn Width="70" Header="Podlaží" DisplayMemberBinding="{Binding Path=podlazi}"/>
<GridViewColumn Width="70" Header="Účel" DisplayMemberBinding="{Binding Path=ucel}"/>
<GridViewColumn Width="70" Header="Plocha" DisplayMemberBinding="{Binding Path=plocha}"/>
<GridViewColumn Width="70" Header="Stav" DisplayMemberBinding="{Binding Path=stav}"/>
<GridViewColumn Width="70" Header="Country" DisplayMemberBinding="{Binding Path=Country}"/>
</GridView>
</ListView.View>
</ListView>
我的代码:
SqlDataAdapter ad = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
String str = "SELECT cislo_Bytu, podlazi, ucel, plocha, stav, poznamky FROM prostory";
cmd.CommandText = str;
ad.SelectCommand = cmd;
cmd.Connection = datovéPřipojení;
DataSet ds = new DataSet();
ad.Fill(ds);
ListViewEmployeeDetails.DataContext = ds.Tables[0].DefaultView;
datovéPřipojení.Close();
所以我的问题是,我应该写入
private void ListViewEmployeeDetails_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
// Here
}
要从点击的行中获取数据(cislo_Bytu
)?
谢谢,
答案 0 :(得分:4)
发件人将是ListView。
((ListView)sender).SelectedItem
由于您绑定了DataView
,SelectedItem
的类型为DataRowView
。然后,您可以使用列名称引用相关值。例如,要将cislo_bytu
列的值分配给textBox1.Text
属性,请执行以下操作:
textBox1.Text = ((DataRowView)((ListView)sender).SelectedItem)["cislo_bytu"].ToString();
答案 1 :(得分:1)
我相信你想要的是OriginalSource MSDN
e.OriginalSource
答案 2 :(得分:0)
使用以下代码通过更改双击事件中ItemArray中的索引值来获取每个项目
((System.Data.DataRowView)(ListViewEmployeeDetails.SelectedItem)).Row.ItemArray[0]
`
答案 3 :(得分:0)
您必须转换所选对象以访问每个属性..
示例:
((Class)listView1.SelectedItem).cislo_bytu
只需将Class更改为具有属性cislo bytu的原始类