WPF以动态/编程方式添加图像以进行控制

时间:2017-12-12 16:13:38

标签: c# wpf xaml wpf-controls staticresource

嘿,我是WPF的新手,所以我需要一些帮助。

我想要做的是动态地将图像添加到我的MainWindow上已经放置的控件上。

我的控件代码是:

<Controls:TransitionPresenter x:Name="_transContainer2"
                                  RestDuration="0:0:3"
                                  IsLooped="True"
                                  Width="200" Height="200"
                                  Transition="{StaticResource SlideTransition}" Margin="16,6,544,787" 
                                  >
        <Image x:Name="_image12"
               Source="Images/img1.png"
               Stretch="Fill" Width="200" Height="200" HorizontalAlignment="Left" />
        <Image x:Name="_image22"
               Source="Images/img2.png"
               Stretch="Fill" Width="200" Height="200" />
        <Image x:Name="_image32"
               Source="Images/img3.png"
               Stretch="Fill" Width="200" Height="200" HorizontalAlignment="Right" />
</Controls:TransitionPresenter>
<Button Content="btnAddImage" HorizontalAlignment="Left" Height="169" Margin="73,525,0,0" VerticalAlignment="Top" Width="180" Click="Button_Click"/>

和Button_Click:

private void Button_Click(object sender, RoutedEventArgs e)
{
    //hum..
}

这是mainWindow背后的代码:

namespace flipwindow
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private string _backItem = "_image1";
        private string _frontItem = "_image2";

        public MainWindow()
        {
            InitializeComponent();
            //Loaded += TransitionTester_Loaded;
            SwapFrontAndBack();
            //PlayCube();
        }

        private void TransitionTester_Loaded(object sender, RoutedEventArgs e)
        {
            _transContainer.TransitionCompleted += _transContainer_TransitionCompleted;
        }

        private void _transContainer_TransitionCompleted(object sender, EventArgs e)
        {
            SwapFrontAndBack();
        }

        private void SwapFrontAndBack()
        {
            string temp = _frontItem;
            _frontItem = _backItem;
            _backItem = temp;
        }

        private void PlayCube()
        {
            CubeTransition transition = Resources["CubeTransition"] as CubeTransition;
            //transition.Rotation = Direction.LeftToRight;
            //transition.Rotation = Direction.RightToLeft;
            //transition.Rotation = Direction.TopToBottom;
            transition.Rotation = Direction.BottomToTop;

            _transContainer.Transition = transition;
            _transContainer.ApplyTransition(_frontItem, _backItem);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _transContainer2.image
        }
    }
}

所以我到目前为止所有这一切都已经过去了。不确定如何从后面的代码中调用该控件并添加到&lt;图片标记 ...

我做了一些搜索,但最重要的是我能找到的只是将图像添加到按钮或将图像添加到数据库等...

理想情况下,我想从后面的代码中调用图像,而不是在XAML代码中。

这是trasContainer代码:

using FluidKit.Controls;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace flipwindow
{
    /// <summary>
    /// Interaction logic for TransitionTester.xaml
    /// </summary>
    public partial class TransitionTester : UserControl
    {
        public TransitionTester()
        {
            InitializeComponent();
            Loaded += TransitionTester_Loaded;
        }

        private string _backItem = "_image1";
        private string _frontItem = "_image2";

        private void TransitionTester_Loaded(object sender, RoutedEventArgs e)
        {
            _transContainer.TransitionCompleted += _transContainer_TransitionCompleted;
        }

        private void _transContainer_TransitionCompleted(object sender, EventArgs e)
        {
            SwapFrontAndBack();
        }

        private void SwitchImage(object sender, MouseButtonEventArgs args)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                _transContainer.ApplyTransition("_image2", "_image1");
            }
        }

        private void PlayCube()
        {
            CubeTransition transition = Resources["CubeTransition"] as CubeTransition;
            transition.Rotation = Direction.LeftToRight;
            //transition.Rotation = Direction.RightToLeft;
            //transition.Rotation = Direction.TopToBottom;
            //transition.Rotation = Direction.BottomToTop;

            _transContainer.Transition = transition;
            _transContainer.ApplyTransition(_frontItem, _backItem);
        }

        private void PlayTransition(object sender, RoutedEventArgs args)
        {
            Button b = sender as Button;
            switch (b.Name)
            {
                case "_playCube":
                    PlayCube();
                    break;
                case "_playSlide":
                    PlaySlide();
                    break;
                case "_playFlip":
                    PlayFlip();
                    break;
            }
        }

        private void PlayFlip()
        {
            FlipTransition transition = Resources["FlipTransition"] as FlipTransition;
            transition.Rotation = Direction.LeftToRight;
            //transition.Rotation = Direction.RightToLeft;
            _transContainer.Transition = transition;
            _transContainer.ApplyTransition(_frontItem, _backItem);
        }

        private void PlaySlide()
        {
            SlideTransition transition = Resources["SlideTransition"] as SlideTransition;
            transition.Direction = Direction.LeftToRight;
            //transition.Direction = Direction.RightToLeft;

            _transContainer.Transition = transition;
            _transContainer.ApplyTransition(_frontItem, _backItem);
        }

        private void SwapFrontAndBack()
        {
            string temp = _frontItem;
            _frontItem = _backItem;
            _backItem = temp;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

似乎FluidKit TransitionPresenter是一个ItemsControl。因此,您可以通过向其项目集合添加控件来向其添加新项目。当你想添加一个Image控件时,我们会创建一个新的Image控件并添加它:

private void Button_Click(object sender, RoutedEventArgs e)
{
    _transContainer2.Items.Add(new Image
    {
        Source = new BitmapImage(new Uri("pathToYourImage")),
        Stretch = Stretch.Fill,
        Width = 200,
        Height = 200
    });
}