0

I have created listview in C# and filled it with data from SQL server. But when I assign mouse double click, I don't know how to get clicked data. Please help:

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

And my code:

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

So my question is, what I should write into the

private void ListViewEmployeeDetails_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    // Here
}

To get the data (cislo_Bytu) from the row which was clicked on?

Thanks,

4

4 回答 4

4

发件人将是 ListView。

((ListView)sender).SelectedItem

由于您绑定到 a DataView,因此SelectedItem将是 类型DataRowView。然后,您可以使用列名引用相关值。例如,要将cislo_bytu列的值分配给textBox1.Text属性,请执行以下操作:

textBox1.Text = ((DataRowView)((ListView)sender).SelectedItem)["cislo_bytu"].ToString();
于 2012-06-27T13:12:50.180 回答
1

我相信你想要的是OriginalSource MSDN

e.OriginalSource
于 2012-06-27T13:08:49.573 回答
0

使用以下代码通过在双击事件中更改 ItemArray 中的索引值来获取每个项目

((System.Data.DataRowView)(ListViewEmployeeDetails.SelectedItem)).Row.ItemArray[0]

`

于 2012-06-27T18:09:10.963 回答
0

您必须强制转换选择的对象才能访问每个属性..

例子:

((Class)listView1.SelectedItem).cislo_bytu

只需将 Class 更改为具有 cislo_bytu 属性的原始类

于 2013-01-16T20:53:01.637 回答