WPF 添加图像 dynamically/programmatically 到控件
WPF add images dynamically/programmatically to control
大家好,我是 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..
}
这是主窗口背后的代码:
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
}
}
}
到目前为止,这就是我所了解的全部内容。不太确定如何从后面的代码中调用该控件并添加到 < Image 标签...
我进行了一些搜索,但我能找到的大部分内容只是将图像添加到按钮或将图像添加到数据库等...
理想情况下,我只想从代码后面而不是在 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;
}
}
}
FluidKit TransitionPresenter 似乎是一个 ItemsControl。因此,您可以通过向其项目集合添加控件来向其添加新项目。由于您要添加图像控件,我们创建一个新的图像控件并添加它:
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
});
}
大家好,我是 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..
}
这是主窗口背后的代码:
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
}
}
}
到目前为止,这就是我所了解的全部内容。不太确定如何从后面的代码中调用该控件并添加到 < Image 标签...
我进行了一些搜索,但我能找到的大部分内容只是将图像添加到按钮或将图像添加到数据库等...
理想情况下,我只想从代码后面而不是在 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;
}
}
}
FluidKit TransitionPresenter 似乎是一个 ItemsControl。因此,您可以通过向其项目集合添加控件来向其添加新项目。由于您要添加图像控件,我们创建一个新的图像控件并添加它:
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
});
}