I'm trying to create an image and label with a loop on C#. I created objects using the drag and drop method and I gave names of all objects and create events manuely. But I need to do this on code behind. I need to use the MouseLeftButtonDown event on image objects. How can I create form objects and events on code behind in a loop?
Form Xaml
<Window x:Class="EnerjiKontrolTakipSistemi.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:EnerjiKontrolTakipSistemi"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="1080" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded" >
<Grid>
<Image x:Name="ImageSigorta1" HorizontalAlignment="Left" Height="155" Margin="38,53,0,0" VerticalAlignment="Top" Width="72" Stretch="Fill" MouseLeftButtonDown="ImageSigorta1_MouseLeftButtonDown"/>
<Label x:Name="lblSigorta1Voltaj" Content="Label" HorizontalAlignment="Left" Margin="55,69,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.708,0.278" Foreground="#FFF74B4B" FontWeight="Bold" Background="{x:Null}"/>
<Label x:Name="lblSigorta1Amper" Content="Label" HorizontalAlignment="Left" Margin="55,165,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.708,0.278" FontWeight="Bold" Foreground="#FFF74B4B" Background="{x:Null}"/>
<Image x:Name="ImageSigorta2" HorizontalAlignment="Left" Height="155" Margin="150,53,0,0" VerticalAlignment="Top" Width="72" Stretch="Fill" MouseLeftButtonDown="ImageSigorta2_MouseLeftButtonDown"/>
<Label x:Name="lblSigorta2Voltaj" Content="Label" HorizontalAlignment="Left" Margin="167,69,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.708,0.278" FontWeight="Bold" Foreground="#FFF74B4B" Background="{x:Null}"/>
<Label x:Name="lblSigorta2Amper" Content="Label" HorizontalAlignment="Left" Margin="167,165,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.708,0.278" Foreground="#FFF74B4B" FontWeight="Bold" Background="{x:Null}"/>
<Label x:Name="lblSigorta1Watt" Content="Label" HorizontalAlignment="Left" Margin="51,210,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.708,0.278" FontWeight="Bold" Foreground="#FFF74B4B" Background="{x:Null}"/>
<Label x:Name="lblSigorta2Watt" Content="Label" HorizontalAlignment="Left" Margin="163,210,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.708,0.278" FontWeight="Bold" Foreground="#FFF74B4B" Background="{x:Null}"/>
<Label x:Name="lblSigortaAdi1" Content="Label" HorizontalAlignment="Left" Margin="42,28,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.708,0.278" Foreground="#FFF74B4B" FontWeight="Bold" Background="{x:Null}"/>
<Label x:Name="lblSigortaAdi2" Content="Label" HorizontalAlignment="Left" Margin="154,28,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.708,0.278" FontWeight="Bold" Foreground="#FFF74B4B" Background="{x:Null}"/>
</Grid>
</Window>
Code Behind
List<Tuple<string, string>> SigortaDurumlari = new List<Tuple<string, string>>();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
SigortaDurumlariniCek();
ResimleriGetir();
}
private void SigortaDurumlariniCek()
{
SigortaDurumlari.Add(new Tuple<string, string>("Sigorta1", "false"));
SigortaDurumlari.Add(new Tuple<string, string>("Sigorta2", "true"));
}
private void ResimleriGetir()
{
ImageSigorta1.Source = new BitmapImage(new Uri(@"Images\salter_acik.jpg", UriKind.Relative));
lblSigortaAdi1.Content = "Sigorta 1";
lblSigorta1Voltaj.Content = "220 V";
lblSigorta1Amper.Content = "500 A";
lblSigorta1Watt.Content = "3800W";
ImageSigorta2.Source = new BitmapImage(new Uri(@"Images\salter_kapali.jpg", UriKind.Relative));
lblSigortaAdi2.Content = "Sigorta 2";
lblSigorta2Voltaj.Content = "220 V";
lblSigorta2Amper.Content = "500 A";
lblSigorta2Watt.Content = "5400W";
}
bool sigorta_durumu1 = false;
private void ImageSigorta1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (sigorta_durumu1 == false)
{
ImageSigorta1.Source = new BitmapImage(new Uri(@"Images\salter_kapali.jpg", UriKind.Relative));
sigorta_durumu1 = true;
}
else
{
ImageSigorta1.Source = new BitmapImage(new Uri(@"Images\salter_acik.jpg", UriKind.Relative));
sigorta_durumu1 = false;
}
}
bool sigorta_durumu2 = false;
private void ImageSigorta2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (sigorta_durumu2 == false)
{
ImageSigorta2.Source = new BitmapImage(new Uri(@"Images\salter_kapali.jpg", UriKind.Relative));
sigorta_durumu2 = true;
}
else
{
ImageSigorta2.Source = new BitmapImage(new Uri(@"Images\salter_acik.jpg", UriKind.Relative));
sigorta_durumu2 = false;
}
}
}
Form Runtime Image