从PreviewMouseDown事件获取图像文件路径(WPF / C#)

时间:2018-03-16 00:19:08

标签: c# wpf uri blend

我正在制作一个图像浏览器。我有一个鼠标事件,应该在单击图像(或者在这种情况下为StackPanel的子项)时触发。事件按预期触发。问题是,由于图像的路径文件根据所选择的(在哪个目录中等)而不同,我只是不知道如何获取当前被点击的特定图像的路径文件。该目录取决于加载的文件夹,因此每次路径文件都可能不同。

如果你一直向下滚动到 Image_Load(),我有Pic.Fill,它是对图像填充的矩形的引用。我正在尝试找到 Uri(_,_)的第一个参数,它应该是当前图像的@“路径”。

using System;
using System.IO;
using Microsoft.Win32;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Drawing;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        FolderBrowserDialog fbd = new FolderBrowserDialog();
        List<string> paths = new List<string>();
        string[] extension = new[] { ".jpg", ".png", ".jpeg", ".gif", ".bmp", ".tif", ".tiff" };

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e) {

            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string[] files = Directory.GetFiles(fbd.SelectedPath);
                FileLocation.Text = fbd.SelectedPath;
            }
        }

        private void OpenButton_Click(object sender, RoutedEventArgs e)
        {
            LoadPhotos();
        }

        private void LoadPhotos()
        {

            try
            {
                DirectoryInfo di = new DirectoryInfo(fbd.SelectedPath);
                foreach (var fi in di.GetFiles().Where(f => extension.Contains(f.Extension.ToLower())).ToArray())
                {
                    paths.Add(fi.FullName);
                }

                Photos.ItemsSource = paths;

            } catch (Exception e)
            {
                Console.WriteLine("File not found.", e.Source);  
            }

        }

        private void Image_Load(object sender, MouseButtonEventArgs e)
        {
                // What is the reference here?
                Pic.Fill = new ImageBrush { ImageSource = new BitmapImage(new Uri(/*Insert pathfile here*/, UriKind.Absolute)) };
         }

    }
}

这是我的.xaml。我已经对它进行了分析以查看引用,但唯一突出的是Image Source,我用注释标记了它。它被设置为{Binding。无论引用什么图像,我都会假设它。但是如何将该变量引用连接到我的 Image_Load()? (在ListBox的实例化中声明)

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="1081" Width="1720.5">
        <Grid Margin="0,0,2,-0.578">
        <Button x:Name="OpenButton" Content="Load" HorizontalAlignment="Left" Height="44" Margin="1166.374,932.042,0,0" VerticalAlignment="Top" Width="145.666" Background="#FFDDDDDD" FontSize="24" Click="OpenButton_Click"/>
        <Button x:Name="BrowseButton" Content="Browse" HorizontalAlignment="Left" Height="44" Margin="58.666,932.042,0,0" VerticalAlignment="Top" Width="134.166" Background="#FFDDDDDD" FontSize="24" Click="Button_Click"/>
        <Rectangle x:Name="Pic" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="878.767" Margin="58.666,34.733,0,0" Stroke="Black" VerticalAlignment="Top" Width="1253.374"/>    
        <ScrollViewer VerticalScrollBarVisibility="Visible" Width="327.46" HorizontalAlignment="Left" Margin="1339.04,34.733,0,137.078" RenderTransformOrigin="0.5,0.5" >
            <StackPanel Width="327.46" RenderTransformOrigin="0.516,0.496" HorizontalAlignment="Left" VerticalAlignment="Center" Height="878.085">    
                <ListBox x:Name="Photos" 
                 Grid.Row="2" Grid.Column="1" Height="876" PreviewMouseDown="Image_Load">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid Width="300" Height="100">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>

                                /* Image Source? */
                                <Image Source="{Binding .}" Grid.Column="0"/>


                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>     
            </StackPanel>
        </ScrollViewer>
        <TextBlock x:Name="FileLocation" HorizontalAlignment="Left" Height="44" Margin="192.832,948.542,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="973.542" Foreground="#FF515151" TextAlignment="Center" FontSize="14"/>
    </Grid>
</Window>

我一直在学习使用Blend的方式 - 这里和那里有一些youtube教程。我试图在线寻找解决方案,但所有关于它的问题通常都涉及一个静态和特定的目录。这是我的第一个程序,我唯一需要的是darn路径文件引用。有太多的对象引用,所以我甚至不知道如何绕过它,对这些类是如此不熟悉。一些帮助将非常感谢! :)

0 个答案:

没有答案