C# Wpf Create Image and labels with for loop - c#

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
Image Link

Related

My first project using C# & WPF - how to set a variable from .xaml.cs to appear in the textbox?

I'm trying to build a calculator with C# and WPF.
The purpose is to learn and test new things.
Here's the code:
XAML
<Window x:Class="calculator.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:calculator"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="400">
<Grid>
<Button Content="9" HorizontalAlignment="Left" Margin="295,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click9"/>
<Button Content="8" HorizontalAlignment="Left" Margin="166,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click8"/>
<Button Content="7" HorizontalAlignment="Left" Margin="37,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click7"/>
<Button Content="6" HorizontalAlignment="Left" Margin="295,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click6"/>
<Button Content="5" HorizontalAlignment="Left" Margin="166,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click5"/>
<Button Content="4" HorizontalAlignment="Left" Margin="37,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click4"/>
<Button Content="3" HorizontalAlignment="Left" Margin="295,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click3"/>
<Button Content="2" HorizontalAlignment="Left" Margin="166,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click2"/>
<Button Content="1" HorizontalAlignment="Left" Margin="37,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click1"/>
<Button Content="0" HorizontalAlignment="Left" Margin="37,316,0,0" VerticalAlignment="Top" Width="321" FontFamily="Arial" Click="Button_Click0"/>
<TextBox HorizontalAlignment="Left" Height="35" Margin="37,49,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="321" FontFamily="Arial" FontSize="20" TextAlignment="Right"/>
<Button Content="Calculate!" HorizontalAlignment="Left" Margin="154,10,0,0" VerticalAlignment="Top" Width="75"/>
<Button Content="+" HorizontalAlignment="Left" Margin="37,125,0,0" VerticalAlignment="Top" Width="68" Height="57" Click="Button_Click_Add"/>
<Button Content="/" HorizontalAlignment="Left" Margin="290,125,0,0" VerticalAlignment="Top" Width="68" Height="57" Click="Button_Click_Multiply"/>
<Button Content="*" HorizontalAlignment="Left" Margin="206,125,0,0" VerticalAlignment="Top" Width="69" Height="57" Click="Button_Click_Divide"/>
<Button Content="-" HorizontalAlignment="Left" Margin="122,125,0,0" VerticalAlignment="Top" Width="68" Height="57" Click="Button_Click_Subtract"/>
</Grid>
</Window>
XAML.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace calculator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
List<int> lista = new List<int>();
string number = "";
int res = 0;
private void Button_Click_Add(object sender, RoutedEventArgs e)
{
int numberCreated;
try
{
numberCreated = Int32.Parse(number);
lista.Add(numberCreated);
}
catch (Exception)
{
MessageBox.Show("Number could not be added!");
}
number = "";
if(lista.Count != 0)
{
foreach(var element in lista)
{
res += element;
}
lista.Clear();
}
MessageBox.Show(res.ToString());
}
private void Button_Click_Subtract(object sender, RoutedEventArgs e)
{
try
{
int numberCreated = Int32.Parse(number);
lista.Add(numberCreated);
}
catch(Exception)
{
MessageBox.Show("Number could not be added!");
}
number = "";
if (lista.Count != 0)
{
foreach (var element in lista)
{
res -= element;
}
lista.Clear();
}
MessageBox.Show(res.ToString());
}
private void Button_Click_Multiply(object sender, RoutedEventArgs e)
{
try
{
int numberCreated = Int32.Parse(number);
lista.Add(numberCreated);
}
catch (Exception)
{
MessageBox.Show("Number could not be added!");
}
number = "";
if (lista.Count != 0)
{
foreach (var element in lista)
{
res *= element;
}
lista.Clear();
}
MessageBox.Show(res.ToString());
}
private void Button_Click_Divide(object sender, RoutedEventArgs e)
{
try
{
int numberCreated = Int32.Parse(number);
lista.Add(numberCreated);
}
catch (Exception)
{
MessageBox.Show("Number could not be added");
}
number = "";
if (lista.Count != 0)
{
foreach (var element in lista)
{
res /= element;
}
lista.Clear();
}
MessageBox.Show(res.ToString());
}
private void Button_Click9(object sender, RoutedEventArgs e)
{
number += "9";
}
private void Button_Click8(object sender, RoutedEventArgs e)
{
number += "8";
}
private void Button_Click7(object sender, RoutedEventArgs e)
{
number += "7";
}
private void Button_Click6(object sender, RoutedEventArgs e)
{
number += "6";
}
private void Button_Click5(object sender, RoutedEventArgs e)
{
number += "5";
}
private void Button_Click4(object sender, RoutedEventArgs e)
{
number += "4";
}
private void Button_Click3(object sender, RoutedEventArgs e)
{
number += "3";
}
private void Button_Click2(object sender, RoutedEventArgs e)
{
number += "2";
}
private void Button_Click1(object sender, RoutedEventArgs e)
{
number += "1";
}
private void Button_Click0(object sender, RoutedEventArgs e)
{
number += "0";
}
}
}
I'm open to any tips and ideas you might give to an absolute beginner.
The reason I'm posting this is because I don't know how to make the "res" variable appear in the TextBox after each operation.
I saw online many people talking about TextBox.text, but I don't know how to use it.
Probably I have to use it in .xaml.cs, but how can I define TextBox? How will my program know which TextBox I want the text to be applied to?
Also, I feel like there's a better approach to create a number using the buttons rather than creating 10 separate functions (from button_click0 to button_click9) and I'd like to hear some ideas.
Thanks!
There are several ways to do this, e.g. Data Binding.
But I guess for learning, since this is your very first step in WPF, you should start more easy.
To access a control via code, you have to give it a name in XAML:
<TextBox x:Name="TxtResult" HorizontalAlignment="Left" Height="35" Margin="37,49,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="321" FontFamily="Arial" FontSize="20" TextAlignment="Right"/>
By that, you can access your Textbox in Code:
private void Button_Click9(object sender, RoutedEventArgs e)
{
number += "9";
this.TxtResult.Text = number;
}
EDIT to answer your last question:
Also for this there are many ways how to make it better or easier. If you want, you can do some research about Commands, Command Parameters and Command Bindings.
But for this simple Demo case, you can simply use the same event handler for all number buttons:
private void ButtonNumberClick(object sender, RoutedEventArgs e)
{
number += ((Button) sender).Content;
this.TxtResult.Text = number;
}
The sender parameter contains the UI object that triggered the event handler, so you'll get a reference to the button that was clicked. And since the Content property of each button already contains the number you want to add to your string, you can grab it from there and append it to the number string.
Now in XAML you just have to assign that event handler to all ten buttons:
<Button Content="9" HorizontalAlignment="Left" Margin="295,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="8" HorizontalAlignment="Left" Margin="166,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="7" HorizontalAlignment="Left" Margin="37,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="6" HorizontalAlignment="Left" Margin="295,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="5" HorizontalAlignment="Left" Margin="166,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="4" HorizontalAlignment="Left" Margin="37,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="3" HorizontalAlignment="Left" Margin="295,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="2" HorizontalAlignment="Left" Margin="166,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="1" HorizontalAlignment="Left" Margin="37,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="0" HorizontalAlignment="Left" Margin="37,316,0,0" VerticalAlignment="Top" Width="321" FontFamily="Arial" Click="ButtonNumberClick"/>

WPF Buttons: How to fix double clicking to trigger click event

I have a WPF layout that consists of several buttons in a toolbar. When the "New Item" button is clicked, another row of text boxes is created. However, the delete button has to be clicked on two times in order for the last created row to be deleted. How would I make the click event for the "Delete" work for a single click and not a double click?
Here is the XMAL code:
<Window x:Class="Odan_Estimator_Tool.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:Odan_Estimator_Tool"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<Grid ScrollViewer.CanContentScroll="True" Name="GridMain">
<DockPanel>
<ToolBarTray DockPanel.Dock="Top">
<ToolBar Height="40" Margin="0,0,-1,0">
<Button Content="New Item" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.512,0.521" Height="19" Click="NewItem" Margin="0,10,0,0"/>
<Button Content="Delete" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Height="19" Margin="0,10,0,0" Click="DeleteItem"/>
<Button Content="Calculate" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.203,0.604" Height="19" Margin="0,9,0,0" />
</ToolBar>
</ToolBarTray>
</DockPanel>
<TextBox HorizontalAlignment="Left" Height="23" Margin="10,76,0,0" TextWrapping="Wrap" Text="{Binding Id, Mode=TwoWay}" VerticalAlignment="Top" Width="145"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="606,76,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="176"/>
<Label Content="Item #" HorizontalAlignment="Left" Margin="10,45,0,0" VerticalAlignment="Top" Height="26" Width="145"/>
<Label Content="Description" HorizontalAlignment="Left" Margin="262,45,0,0" VerticalAlignment="Top" Height="26" Width="226"/>
<Label Content="Unit" HorizontalAlignment="Left" Margin="160,45,0,0" VerticalAlignment="Top" RenderTransformOrigin="-1.725,-4.203" Height="26" Width="102"/>
<Label Content="Type" HorizontalAlignment="Left" Margin="493,45,0,0" VerticalAlignment="Top" RenderTransformOrigin="2.705,2.641" Height="26" Width="113"/>
<Label Content="Est. Qty" HorizontalAlignment="Left" Margin="611,45,0,0" VerticalAlignment="Top" RenderTransformOrigin="4.5,1.203" Height="26" Width="158"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="259,76,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="229" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="155,76,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="104" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="488,76,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="118" RenderTransformOrigin="1.403,0.47"/>
<ScrollViewer HorizontalAlignment="Left" Height="245" Margin="914,154,-506,0" VerticalAlignment="Top" Width="384"/>
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="45" VerticalAlignment="Top" Width="792"/>
</Grid>
</ScrollViewer>
Here is the C# code with the Delete function at the bottom.
private void NewItem(object sender, RoutedEventArgs e)
{
//Generates new row of text boxes
//Generates new item ID textbox
TextBox item_box = new TextBox();
item_box.Height = 23;
item_box.Width = 161;
item_box.Text = "";
item_box.Margin = new Thickness(21, 76 + 23*counter, 0, 0);
item_box.VerticalAlignment = 0;
item_box.HorizontalAlignment = 0;
items.Add(item_box);
GridMain.Children.Add(item_box);
//Generates new item ID textbox
TextBox id_box = new TextBox();
id_box.Height = 23;
id_box.Width = 161;
id_box.Text = "";
id_box.Margin = new Thickness(21, 76 + 23 * counter, 0, 0);
id_box.VerticalAlignment = 0;
id_box.HorizontalAlignment = 0;
items.Add(id_box);
GridMain.Children.Add(id_box);
counter++;
}
private void DeleteItem(object sender, RoutedEventArgs e)
{
GridMain.Children.Remove(items.ElementAt(items.Count - 1));
items.RemoveAt(items.Count - 1);
}

how to properly use item click in gridview

I have a gridview, which presents all of the users I have in my database, and their details, I want to be able, on the click of a user(on the gridview), that it's details will appear separately on the side of the grid view, and there I could change them accordingly. However, I'm not sure how to do so. I have the ItemClick event in my gridview, and It does recognize if I click on an item, but I don't know how to get that specific clicked user's details, for me to present them on the side. I also have another problem- for some reason It doesn't recognize nor let me click on any of the top row items on the grid view-only the ones below it, does anyone know why?
this is my XAML :
<Page
x:Class="My_Little_Animal.ShowUsers"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:My_Little_Animal"
xmlns:data="using:My_Little_Animal"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid.Background>
<ImageBrush ImageSource="/Assets/changeUserDetailesBackground.jpg"></ImageBrush>
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="8*" />
<RowDefinition Height="147*"/>
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<GridView Name="grid1" ItemClick="grid1_ItemClick" IsItemClickEnabled="True" Margin="-70,151,692,50" Grid.RowSpan="2" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<GridView.ItemTemplate>
<DataTemplate x:DataType="data:User">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<StackPanel Margin="100,20,0,0">
<TextBlock Name="t1" FontSize="20" GotFocus="t1_GotFocus" Text="{x:Bind UserId}"/>
<TextBlock Name="t2" FontSize="20" GotFocus="t2_GotFocus" Text="{x:Bind Password}"/>
<TextBlock Name="t3" FontSize="20" GotFocus="t3_GotFocus" Text="{x:Bind UserName}"/>
<TextBlock Name="t4" FontSize="20" GotFocus="t4_GotFocus" Text="{x:Bind Email}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
<Canvas>
<Image Name="loginTitle" Source="/Assets/ShowUsersTitle.png" RenderTransformOrigin="0.5,0.5" Grid.RowSpan="2" Canvas.Left="-325" Canvas.Top="-120" Height="462" Width="1875"/>
<TextBlock Grid.Row="2" Name="ResultTextBlock" FontSize="24" Foreground="Red" FontWeight="Bold" Height="169" Margin="0,0,0,-69"/>
<Image Name="editTheUserName" Width="100" Height="35" Visibility="Visible" Source="/Assets/editIcon.png" RenderTransformOrigin="0.5,0.5" Grid.Row="1" Canvas.Left="852" Canvas.Top="226" />
<Image Name="CancelTheUserNamE" Width="100" Height="35" Visibility="Visible" Source="/Assets/xIcon.png" RenderTransformOrigin="0.485,0.529" Grid.Row="1" Canvas.Left="848" Canvas.Top="224" />
<TextBlock Visibility="Visible" Name="theUserItself" FontFamily="Comic Sans MS" FontWeight="ExtraBold" FontStyle="Italic" FontSize="25" Text="your user name " RenderTransformOrigin="0.496,1.165" Foreground="#FF283D6C" Grid.Row="1" Canvas.Left="1103" Canvas.Top="219" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch"/>
<TextBlock Name="userName" FontFamily="Comic Sans MS" FontWeight="ExtraBold" FontStyle="Italic" FontSize="25" Text="User Name : " RenderTransformOrigin="0.496,1.165" TextDecorations="Underline" Grid.Row="1" Canvas.Left="922" Canvas.Top="224"/>
<TextBox Visibility="Visible" Width="200" Name="UserNameText" MaxLength="50" FontFamily="Comic Sans MS" FontStyle="Italic" FontSize="15" BorderThickness="3" BorderBrush="#FF23677D" Grid.Row="1" Canvas.Left="1099" Canvas.Top="223" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch"/>
<Button Visibility="Visible" Name="changeuserName" Height="49" Width="140" FontSize="20" Content="change" FontWeight="ExtraBold" BorderBrush="White" Foreground="White" BorderThickness="2" Background="#FF191A5C" HorizontalAlignment="Center" VerticalAlignment="Top" RenderTransformOrigin="0.45,0.536" Grid.Row="1" Canvas.Left="1305" Canvas.Top="210" Margin="0,0,0,0"/>
<Image Name="editTheEmail" Canvas.Top="286" Canvas.Left="850" Width="100" Height="35" Visibility="Visible" Source="/Assets/editIcon.png" RenderTransformOrigin="0.49,0.557" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<Image Name="CancelTheEmail" Width="100" Height="35" Visibility="Visible" Source="/Assets/xIcon.png" RenderTransformOrigin="0.485,0.529" Grid.Row="1" Canvas.Left="847" Canvas.Top="284" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<TextBlock Visibility="Visible" Name="theEmailItself" FontFamily="Comic Sans MS" FontWeight="ExtraBold" FontStyle="Italic" FontSize="25" Text="your email " RenderTransformOrigin="0.496,1.165" Foreground="#FF283D6C" Grid.Row="1" Canvas.Left="1105" Canvas.Top="276"/>
<TextBlock Name="Email" FontFamily="Comic Sans MS" FontWeight="ExtraBold" FontStyle="Italic" FontSize="25" Text="Email : " RenderTransformOrigin="0.496,1.165" TextDecorations="Underline" Grid.Row="1" Canvas.Left="921" Canvas.Top="283" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<TextBox Visibility="Visible" Width="200" Name="EmailText" MaxLength="50" FontFamily="Comic Sans MS" FontStyle="Italic" FontSize="15" BorderThickness="3" BorderBrush="#FF23677D" Grid.Row="1" Canvas.Left="1093" Canvas.Top="284" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch"/>
<Button Visibility="Visible" Name="changeEmail" Height="49" Width="140" FontSize="20" Content="change" FontWeight="ExtraBold" BorderBrush="White" Foreground="White" BorderThickness="2" Background="#FF191A5C" HorizontalAlignment="Center" VerticalAlignment="Top" RenderTransformOrigin="0.45,0.536" Grid.Row="1" Canvas.Left="1302" Canvas.Top="274" Margin="0,0,0,0"/>
<Image Name="theReturn" Height="100" Width="100" Tapped="theReturn_Tapped_1" Source="/Assets/theReturnIcon.png" RenderTransformOrigin="0.493,0.496" Grid.RowSpan="2" Canvas.Left="65" Canvas.Top="27" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Canvas>
</Grid>
The c# code :
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Xml;
using System.Data;
using Windows.UI.Popups;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238
namespace My_Little_Animal
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class ShowUsers : Page
{
regitration.regitrationSoapClient cal;
public ShowUsers()
{
this.InitializeComponent();
cal = new regitration.regitrationSoapClient();
getData();
}
public async void getData()
{
regitration.getUserTableResponseGetUserTableResult r = await cal.getUserTableAsync();
List<User> theUserList = new List<User>();
User u = null;
XmlReader xr = r.Any1.CreateReader();
XmlDocument document = new XmlDocument();
document.Load(xr);
XmlNodeList theXmlList = document.GetElementsByTagName("Table");
foreach (XmlElement item in theXmlList)
{
u = new User();
foreach (XmlNode node in item.ChildNodes)
{
switch (node.Name)
{
case "userId": u.UserId = int.Parse(node.InnerText); break;
case "password": u.Password = node.InnerText; break;
case "userName": u.UserName = node.InnerText; break;
case "email": u.Email = node.InnerText; break;
}
}
theUserList.Add(u);
}
grid1.ItemsSource = theUserList;
}
private async void grid1_ItemClick(object sender, ItemClickEventArgs e)
{
var dialog = new MessageDialog("The item that was clicked is : ");
await dialog.ShowAsync();
}
private void t1_GotFocus(object sender, RoutedEventArgs e)
{
}
private void t2_GotFocus(object sender, RoutedEventArgs e)
{
}
private void t3_GotFocus(object sender, RoutedEventArgs e)
{
}
private void t4_GotFocus(object sender, RoutedEventArgs e)
{
}
private void theReturn_Tapped_1(object sender, TappedRoutedEventArgs e)
{
this.Frame.Navigate(typeof(Administrator));
}
}
}
I will be so thankful for any help, it's really important for me!
but I don't know how to get that specific clicked user's details, for me to present them on the side.
There are many ways to get the item in item click event. if you have used MVVM pattern, the recommend way is binding command. For more you could refer this case.
In general, you could subscribe ItemClick event and get ClickedItem from event ItemClickEventArgs
Example
private void VideoGridView_ItemClick(object sender, ItemClickEventArgs e)
{
var vedio = e.ClickedItem as VideoItem;
var item = new MediaPlaybackItem(MediaSource.CreateFromUri(new Uri(vedio.videoUri)));
mediaPlayerElement.Source = item;
}
I also have another problem- for some reason It doesn't recognize nor let me click on any of the top row items on the grid view-only the ones below it, does anyone know why?
I could not reproduce this issue, please check if there is some element cover the top row up cause the grid view could not be clicked.

Getting the parent of usercontrol inside Dispatcher.BeginInvoke

Am having a Main window in my WPF application inside which am having a tabcontrol.I need to select the tabitem based on the name i pass.Each of the tabitem will contain a user control.But my problem is am not able to get the parent of the usercontrol using Parent property.Its returning null only always.Can anyone help me out.
public void selectingTab()
{
Dispatcher.BeginInvoke((Action)(() =>
{
string name = todisplayChat.GetValue<string>("FromMessenger");
string msg = todisplayChat.GetValue<string>("ChatSent");
NewChatWindow childWindow = this as NewChatWindow;
MainChatWindow parentWindow = VisualTreeHelper.GetParent(childWindow) as MainChatWindow;
//Am getting null as the value of parentWindow after the execution of this line
TabControl tabcontrolHome = parentWindow.tabcontrol_Chatwindow;
TabItem existingTab = tabcontrolHome.Items.OfType<TabItem>().SingleOrDefault(n => n.Name == name);
childWindow = (NewChatWindow)existingTab.Content;
childWindow.richtxtbox_chatwindow.AppendText(Environment.NewLine + name + " : " + msg);
}));
}
MainChatWindow.XAML
<Window x:Class="Ping.MainChatWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="600" Width="700" Icon="F:\Dhivya\C#\Personal\ChatApplication\CurrentlyWorking-Important\Ping\Ping\images\ping_logo.png" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen" WindowStyle="None" Foreground="{x:Null}" BorderBrush="#FF87A4DB" BorderThickness="2">
<Grid>
<Label Content="Ping" Height="30" HorizontalAlignment="Left" Name="lbl_Titlebar" VerticalAlignment="Top" Width="700" Background="#FF87A4DB" FontFamily="Arial" FontSize="17" Foreground="White" />
<Image Height="20" HorizontalAlignment="Left" Margin="635,5,0,0" Name="img_Minimizebutton" Stretch="Fill" VerticalAlignment="Top" Width="20" Source="F:\Dhivya\C#\Personal\ChatApplication\CurrentlyWorking-Important\Ping\Ping\images\minimize_userpage.png" />
<!--<Label Height="25" HorizontalAlignment="Left" Margin="665,5,0,0" VerticalAlignment="Top" Width="20">-->
<Image MouseEnter="close_icon_MouseEnter_1" MouseDown="close_icon_MouseDown_1" Height="20" HorizontalAlignment="Left" Margin="665,5,0,0" Name="close_icon" Stretch="Fill" VerticalAlignment="Top" Width="20" Source="F:\Dhivya\C#\Personal\ChatApplication\CurrentlyWorking-Important\Ping\Ping\images\close_userpage.png" />
<!--</Label>-->
<Label Content="Groups" Height="28" HorizontalAlignment="Left" Margin="12,46,0,0" Name="lbl_Groups" VerticalAlignment="Top" Background="#FF87A4DB" Foreground="White" BorderBrush="{x:Null}" Width="245" FontSize="16" FontFamily="Arial" />
<RichTextBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Height="175" HorizontalAlignment="Left" Margin="12,73,0,0" Name="richtxtbox_Groups" VerticalAlignment="Top" Width="245" FontFamily="Arial" />
<Label Background="#FF87A4DB" BorderBrush="{x:Null}" Content="Online Buddies" FontFamily="Arial" FontSize="16" Foreground="White" Height="28" HorizontalAlignment="Left" Margin="12,260,0,0" Name="lbl_Onlinebuddies" VerticalAlignment="Top" Width="245" />
<Grid ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Height="298" HorizontalAlignment="Left" Margin="12,288,0,0" Name="richtxtbox_Onlinebuddies" VerticalAlignment="Top" Width="245"></Grid>
<!--<RichTextBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Height="298" HorizontalAlignment="Left" Margin="12,288,0,0" Name="richtxtbox_Onlinebuddies" VerticalAlignment="Top" Width="245" FontFamily="Arial" />-->
<TabControl Height="540" HorizontalAlignment="Left" Margin="274,46,0,0" Name="tabcontrol_Chatwindow" VerticalAlignment="Top" Width="410">
<TabItem Header="My Profile" Name="ProfileTab">
</TabItem>
</TabControl>
</Grid>
NewChatWindow.Xaml
<UserControl x:Class="Ping.NewChatWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="540" d:DesignWidth="410">
<Grid>
<TextBox LostFocus="txt_Userinput_LostFocus_1" Foreground="Gray" GotFocus="txt_Userinput_GotFocus_1" KeyDown="txt_Userinput_KeyDown_1" Height="65" HorizontalAlignment="Left" Margin="13,161,0,0" Name="txt_Userinput" VerticalAlignment="Top" Width="378" FontFamily="Arial" Text="Enter Your Words Here" />
<RichTextBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Height="246" HorizontalAlignment="Left" Margin="13,253,0,0" Name="richtxtbox_chatwindow" VerticalAlignment="Top" Width="378" FontFamily="Arial" />
<Image Height="93" HorizontalAlignment="Left" Margin="36,36,0,0" Name="image2" Stretch="Fill" VerticalAlignment="Top" Width="65" Source="F:\Dhivya\C#\Personal\ChatApplication\CurrentlyWorking-Important\Ping\Ping\images\user_logo.png"/>
<Label Content="Label" Height="37" HorizontalAlignment="Left" Margin="124,24,0,0" Name="lbl_Username" VerticalAlignment="Top" Width="278" FontSize="24" />
<Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="125,52,0,0" Name="lbl_Statusmsg" VerticalAlignment="Top" Width="284" />
<Image Height="40" Width="30" HorizontalAlignment="Left" Margin="131,89,0,0" Name="img_Profileview" Stretch="Fill" VerticalAlignment="Top" Source="F:\Dhivya\C#\Personal\ChatApplication\CurrentlyWorking-Important\Ping\Ping\images\user_logo.png" ToolTip="Add contact"/>
<Image MouseDown="img_Sendingfile_MouseDown_1" Height="40" Width="40" HorizontalAlignment="Left" Margin="191,89,0,0" Name="img_Sendingfile" Stretch="Fill" VerticalAlignment="Top" Source="F:\Dhivya\C#\Personal\ChatApplication\CurrentlyWorking-Important\Ping\Ping\images\sendingfile_icon.png" ToolTip="Fileupload"/>
<Image Height="40" Width="40" HorizontalAlignment="Left" Margin="261,89,0,0" Name="img_Videochat" Stretch="Fill" VerticalAlignment="Top" Source="F:\Dhivya\C#\Personal\ChatApplication\CurrentlyWorking-Important\Ping\Ping\images\videochat_logo.png" ToolTip="Video chat"/>
<Image MouseDown="img_Myfolder_MouseDown_1" Height="40" Width="40" Name="img_Myfolder" Stretch="Fill" Source="F:\Dhivya\C#\Personal\ChatApplication\CurrentlyWorking-Important\Ping\Ping\images\myfolder_logo.png" Margin="329,89,41,411" ToolTip="View chat history" />
<Border BorderBrush="#FF87A4DB" BorderThickness="1" HorizontalAlignment="Left" Margin="10,18,0,367" Name="border_Userdetails" Width="378" CornerRadius="2" />
</Grid>
Am having a set of labels for the online users.So when i double click any label i ll do the following
private void Label_MouseDoubleClick_1(object sender, MouseButtonEventArgs e)
{
Label selectedLabel = (Label)sender;
TabItem existingTab = tabcontrol_Chatwindow.Items.OfType<TabItem>().SingleOrDefault(n => n.Name == selectedLabel.Content);
if (tabcontrol_Chatwindow.Items.Contains(existingTab))
{
tabcontrol_Chatwindow.SelectedItem = existingTab;
}
else
{
TabItem newTab = new TabItem();
newTab.Name = selectedLabel.Content.ToString();
TabHeader header = new TabHeader();
header.userName.Content = selectedLabel.Content;
newTab.Header = header;
//newTab.Header = selectedLabel.Content;
newTab.Content = new NewChatWindow();
tabcontrol_Chatwindow.Items.Add(newTab);
tabcontrol_Chatwindow.SelectedItem = newTab;
}
}
Try using the below code, in which you can find the Window hosting your Usercontrol using Window parentWindow = Window.GetWindow(this);
Dispatcher.BeginInvoke((Action)(() =>
{
string name = todisplayChat.GetValue<string>("FromMessenger");
string msg = todisplayChat.GetValue<string>("ChatSent");
NewChatWindow childWindow = this as NewChatWindow;
MainChatWindow parentWindow = Window.GetWindow(this) as MainChatWindow;
TabControl tabcontrolHome = parentWindow.tabcontrol_Chatwindow;
TabItem existingTab = tabcontrolHome.Items.OfType<TabItem>().SingleOrDefault(n => n.Name == name);
childWindow = (NewChatWindow)existingTab.Content;
childWindow.richtxtbox_chatwindow.AppendText(Environment.NewLine + name + " : " + msg);
}));

Convert rtf to xaml in silverlight

I want to load a rtf file in silverlight using RichTextBox control but this control doesn't load file in proper format. can anyone tell how to do so or convert rtf to xaml?
Here's the xaml :
<Grid x:Name="LayoutRoot" Height="480" Width="640">
<RichTextBox HorizontalAlignment="Left" Margin="89,64,0,0" Name="rtb" VerticalAlignment="Top" Height="301" Width="416"/>
<Button Content="Select File" Height="23" HorizontalAlignment="Left" Margin="268,12,0,0" Name="btnSelect" VerticalAlignment="Top" Width="75" Click="btnSelect_Click" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBlock1" Text="FileName:" VerticalAlignment="Top" Width="73" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="89,12,0,0" Name="txtFile" VerticalAlignment="Top" Width="164" />
<Button Content="Reset" Height="23" HorizontalAlignment="Left" Margin="430,385,0,0" Name="btnReset" VerticalAlignment="Top" Width="75" Click="btnReset_Click" IsEnabled="False" />
</Grid>
Code behind :
private void btnSelect_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog odlg = new OpenFileDialog();
odlg.Filter = "Text files|*.rtf";
odlg.Multiselect = false;
string contents = null;
if ((bool)odlg.ShowDialog())
{
txtFile.Text = odlg.File.Name;
StreamReader reader = new StreamReader(odlg.File.OpenRead());
while (!reader.EndOfStream)
{
contents = reader.ReadToEnd();
}
reader.Close();
rtb.Selection.Text = contents;
btnReset.IsEnabled = true;
}
}
private void btnReset_Click(object sender, RoutedEventArgs e)
{
txtFile.Text = "";
rtb.Blocks.Clear();
}

Categories

Resources