我有一个包含少量图像项的ListView。我希望在字符串中单击它时获取项目路径(图像)并将其作为VALUES进入数据库。谢谢。
XML
<ListView x:Name="LvImagini" ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalAlignment="Left" Height="200" Margin="215,299,0,0" VerticalAlignment="Top" Width="200">
<ListViewItem>
<Image Source="/imagini/1.png" Width="170" Height="180"/>
</ListViewItem>
<ListViewItem>
<Image Source="/imagini/2.png" Width="170" Height="180"/>
</ListViewItem>
</ListView>
按钮功能
private void btnRegSucces_Click(object sender, RoutedEventArgs e)
{
try
{
con.Open();
String query = "INSERT into jucatori(utilizator,password) VALUES('" + tbxUtilizator.Text + "','" + tbxParola.Password + "')";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
sda.SelectCommand.ExecuteNonQuery();
con.Close();
//String geo = LvImagini.SelectedItem.ToString();
}
catch
{
LabelError.Content = "E R O A R E!";
}
}
答案 0 :(得分:0)
我假设您在从所选图像获取源属性时遇到麻烦,而不是实际的数据库查询。
private void ListViewItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var source= ((Image)((ListViewItem)sender).Content).Source;
}
XAML
<ListView HorizontalAlignment="Left" Height="257" Margin="135,52,0,0" VerticalAlignment="Top" Width="322">
<ListViewItem>
<Image Source="\imagini\1.jpg" Width="170" Height="180"/>
</ListViewItem>
<ListViewItem>
<Image Source="\imagini\2.jpg" Width="170" Height="180"/>
</ListViewItem>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
答案 1 :(得分:0)
从XAML标记中获取源代码并不容易,因为它在幕后转换为包URI,但这样的事情应该有效:
ListViewItem selectedItem = LvImagini.SelectedItem as ListViewItem;
Image image = selectedItem.Content as Image;
string source = image.Source.ToString();
string path = source.Substring(source.IndexOf("pack://application:,,,/") + 23);