こんにちはすべて私はWPFで新しくなったので、少し助けが必要です。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
}
}
}
私が今までに得たことのほとんどすべて。私はいくつかの検索をしたきた方法の背後にあるコードからそのコントロールを呼び出し、<イメージタグに追加する...
本当にわからしかし、私は見つけることができるすべてのほとんどは、単にボタンに画像を追加していませんか、データベースに画像を追加するなど...
理想的には、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;
}
}
}
@Clemens悲しいことに、それはそれを持っていません。 – StealthRT
@Clemens transContainerコードでOPを更新しました。 – StealthRT