Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF动态添加带有图像和样式的按钮_C#_Wpf_Xaml_Attached Properties - Fatal编程技术网

C# WPF动态添加带有图像和样式的按钮

C# WPF动态添加带有图像和样式的按钮,c#,wpf,xaml,attached-properties,C#,Wpf,Xaml,Attached Properties,我的目标如下:我想将一个文件拖放到我的WPF窗口,它应该在运行时创建一个带有图标图像的按钮 目前,我的代码只是创建了一个没有任何图像的按钮。我是WPF的新手,所以请对我放松点 public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.RegisterAttached("ImageSource", typeof(ImageSource), typeof(AttachedProperties)

我的目标如下:我想将一个文件拖放到我的WPF窗口,它应该在运行时创建一个带有图标图像的按钮

目前,我的代码只是创建了一个没有任何图像的按钮。我是WPF的新手,所以请对我放松点

public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.RegisterAttached("ImageSource", typeof(ImageSource), typeof(AttachedProperties), new UIPropertyMetadata(null));

public static void SetImageSource(DependencyObject d, ImageSource source)
{
    d.SetValue(ImageSourceProperty, source);
}


private Bitmap GetIconImageFromFile(String filename)
{
    Bitmap bmp = System.Drawing.Icon.ExtractAssociatedIcon(filename).ToBitmap();
    bmp.Save("test.bmp");
    return bmp;
}

private BitmapImage GetBitmapImageFromBitmap(Bitmap bitmap)
{
    BitmapImage bitmapImage = new BitmapImage();
    using (MemoryStream memory = new MemoryStream())
    {
        bitmap.Save(memory, ImageFormat.Png);
        memory.Position = 0;
        memory.Seek(0, SeekOrigin.Begin);

        bitmapImage.BeginInit();
        bitmapImage.StreamSource = memory;
        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapImage.EndInit();
    }

    return bitmapImage;
}

private void Window_Drop(object sender, System.Windows.DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop) == true)
    {
        String[] fileNames = (String[])e.Data.GetData(DataFormats.FileDrop);


        foreach (String fileName in fileNames)
        {
            MessageBox.Show("WindowDrop " + fileName);

            Button newButton = new Button();
            newButton.Width = 100;
            newButton.Height = 50;
            //newButton.Template = (ControlTemplate)TryFindResource("btemp2");
            Bitmap bmp = GetIconImageFromFile(fileName);
            BitmapSource src = GetBitmapImageFromBitmap(bmp);
            AttachedProperties.SetImageSource(newButton,src);
            stack.Children.Add(newButton);
        }
    }
}

我修改了您的代码如下,我创建了图像实例并将其设置为按钮内容。请试试这个

foreach (String fileName in fileNames)
{
    MessageBox.Show("WindowDrop " + fileName);

    Button newButton = new Button();
    newButton.Width = 100;
    newButton.Height = 50;


    //Craete the Image isntance and set the image to be dispalyed
    Image ButtonPicture = new Image();
    ButtonPicture.BaseUri = fileName;

    //Set it to the button content
    newButton.Content = ButtonPicture;

    AttachedProperties.SetImageSource(newButton,src);
    stack.Children.Add(newButton);
}

我修改了您的代码如下,我创建了图像实例并将其设置为按钮内容。请试试这个

foreach (String fileName in fileNames)
{
    MessageBox.Show("WindowDrop " + fileName);

    Button newButton = new Button();
    newButton.Width = 100;
    newButton.Height = 50;


    //Craete the Image isntance and set the image to be dispalyed
    Image ButtonPicture = new Image();
    ButtonPicture.BaseUri = fileName;

    //Set it to the button content
    newButton.Content = ButtonPicture;

    AttachedProperties.SetImageSource(newButton,src);
    stack.Children.Add(newButton);
}

嗨,它工作了。现在我想给按钮添加一个样式。这种样式只是在边框中有一个图像。我想将此样式应用于按钮。然后,我想让图像的源代码成为我的bitmapHi,它可以工作。现在我想给按钮添加一个样式。这种样式只是在边框中有一个图像。我想将此样式应用于按钮。然后,我想让图像的源成为我的位图我的答案有帮助吗?我的答案有帮助吗?