选择&将图像从Datagrid传递到新窗口

时间:2017-10-03 01:12:47

标签: wpf vb.net

如何从datagrid中选择并传递图像到与mysql连接的新窗口?当我从datagrid行选择图像时,我想在新窗口中查看图像。

MainWindow:XAML

<Grid>
    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" MouseLeftButtonUp="uxDeclaration_MouseDoubleClick" Height="301" Width="527" HorizontalAlignment="Left" Margin="31,35,0,0" x:Name="dtGrid" VerticalAlignment="Top"  Background="White" CanUserAddRows="false">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Id" Binding="{Binding Path=id}"  Width="*"/>
            <DataGridTextColumn Header="ImagePath" Binding="{Binding Path=Name}" Width="*" />
            <DataGridTemplateColumn Header="View Image" Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Image Source="{Binding Path=Image2}" Width="30" Height="30" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

MainWindow:代码

 Imports MySql.Data.MySqlClient
 Imports System.Data

 Class MainWindow
 Public Sub New()

    InitializeComponent()

    Load_datagrid()

End Sub

Dim Mysqlconn As MySqlConnection
Dim COMMAND As MySqlCommand
Dim dt As New DataTable
Dim da As New MySqlDataAdapter


Private Sub Load_datagrid()
    Dim connectionString As String = "server=localhost; 
 database=delete_image; userid=root; password=***;"

    Dim connection As New MySqlConnection(connectionString)
    Dim cmd As New MySqlCommand("Select * from image", connection)
    connection.Open()

    dt.Load(cmd.ExecuteReader())
    connection.Close()

    dtGrid.DataContext = dt
End Sub

SecondWindow:XAML

<Grid>
    <Image x:Name="image1" HorizontalAlignment="Left" Height="373" Margin="10,33,0,0" VerticalAlignment="Top" Width="974"/>

</Grid>

1 个答案:

答案 0 :(得分:0)

最后我明白了,加上这个 - &gt; IsSynchronizedWithCurrentItem =&#34;真&#34;

MainWindow:XAML

<Window x:Class="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:WPF_POPUP"
    mc:Ignorable="d"
    Title="MainWindow" Height="600" Width="1000">
<Grid>
    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" Height="301" Width="527" HorizontalAlignment="Left" Margin="38,164,0,0" x:Name="dtGrid" VerticalAlignment="Top"  Background="White" CanUserAddRows="false"  MouseLeftButtonUp="uxDeclaration_MouseDoubleClick" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="Id" Binding="{Binding Path=id}"  Width="*"/>
            <DataGridTextColumn Header="ImagePath" Binding="{Binding Path=Name}" Width="*" />
            <DataGridTemplateColumn Header="View Image" Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Image Source="{Binding Path=Image2}" Width="30" Height="30" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>

    </DataGrid>
</Grid>

MainWindow:代码

Imports MySql.Data.MySqlClient

Imports System.Data

Class MainWindow     Public Sub New()

    InitializeComponent()

    Load_datagrid()

End Sub

Dim Mysqlconn As MySqlConnection
Dim COMMAND As MySqlCommand
Dim dt As New DataTable
Dim da As New MySqlDataAdapter



Private Sub Load_datagrid()
    Dim connectionString As String = "server=localhost; database=delete_image; userid=root; password=112233;"

    Dim connection As New MySqlConnection(connectionString)
    Dim cmd As New MySqlCommand("Select * from view_image", connection)
    connection.Open()

    dt.Load(cmd.ExecuteReader())
    connection.Close()

    dtGrid.DataContext = dt
End Sub

Private Sub uxDeclaration_MouseDoubleClick(sender As Object, e As MouseButtonEventArgs)


    If True Then
        Dim sec As New secondwindow()
        sec.DataContext = dt
        sec.Show()
    End If

End Sub

SecondWindow:XAML

<Window x:Class="secondwindow"
    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:WPF_POPUP"
    mc:Ignorable="d"
    Title="secondwindow" Height="Auto" Width="Auto">
<Grid>
    <Image x:Name="image1" Source="{Binding Path=Image2}" HorizontalAlignment="Left" Height="Auto" Width="Auto" Margin="10,33,0,0" VerticalAlignment="Top" />
</Grid>

MAINWINDOW SAMPLE PIC

enter image description here (MAINWINDOW SAMPLE)

SECONFWINDOW示例PIC

enter image description here (SECONFWINDOW SAMPLE)