I'm using c# and WPF web application and I want to build a simple form which contains two text boxes for user input (name and phone number) and a "send" button. when the user clicks on the send button it will redirect to an other page and will display (with textblock) the entered values.
I tried to read about data binding, but still didn't success to make it work.
1. How do I save the entered values into a variable ?
2. How do I call these variables from the second page and display the saved text ?
Hope for help, thanks!
xaml code of the form:
<TextBlock Height="20" Width="120" Margin="36,43,144,237">enter details:</TextBlock>
<TextBlock Height="20" Width="40" Margin="36,69,224,211">Name:</TextBlock>
<TextBlock Height="20" Width="40" Margin="36,103,224,177">Phone:</TextBlock>
<!--textboxes-->
<TextBox Height="20" Width="95" Margin="100,69,104,211" Name="getName" Background="Gray"/>
<TextBox Height="20" Width="95" Margin="100,103,105,177" Name="getPhoneNumber" Background="Gray"/>
<!-- -->
<Button Height="20" Width="50" Margin="218,103,32,177" Name="sendButton" Click="sendButton_Click">send</Button>
what should be the code behind ? i simply want to display the entered values on an other page with textblock.
this is the function i use to redirect to the "result page":
public void sendButton_Click(object sender, RoutedEventArgs e)
{
Submit();
}
void Submit()
{
Page2 resultpage = new Page2();
NavigationService.Navigate(resultpage);
}
Edit:
Ok I succeed to make it work thanks to Mike's answer.
Thanks Mike, for other users this how the code looks like now:
form page xaml code:
<TextBlock Height="20" Width="120" Margin="36,43,144,237">enter details:</TextBlock>
<TextBlock Height="20" Width="40" Margin="36,69,224,211">Name:</TextBlock>
<TextBlock Height="20" Width="40" Margin="36,103,224,177">Phone:</TextBlock>
<!--textboxes-->
<TextBox Height="20" Width="95" Margin="100,69,104,211" Name="getName" Background="Gray"/>
<TextBox Height="20" Width="95" Margin="100,103,105,177" Name="getPhoneNumber" Background="Gray"/>
<!-- -->
<Button Height="20" Width="50" Margin="218,103,32,177" Name="sendButton" Click="sendButton_Click">send</Button>
form page c# behind code:
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
}
public void sendButton_Click(object sender, RoutedEventArgs e)
{
Submit();
}
void Submit()
{
Page2 resultpage = new Page2(getName.Text, getPhoneNumber.Text);
NavigationService.Navigate(resultpage);
}
}
result page xaml code:
<Grid>
<TextBlock x:Name="showName" Height="50" Width="100" Margin="65,125,73,125" Text="{Binding ElementName=showName, Path=Text}" />
</Grid>
result page c# code behind:
public Page2(string name, string phoneNumber)
{
InitializeComponent();
showName.Text = name;
}
Thanks again Mike :)
Ok here is what I would do. You can create arguments in the constructor of your Page2:
public Page2(string name, string phoneNumber)
{
//login to handle name and phone number
}
On your first page you can just pass name and phone number using the Name property in the xaml.
void Submit()
{
Page2 resultpage = new Page2(getName.Text, getPhoneNumber.Text);
NavigationService.Navigate(resultpage);
}
<Page x:Class="Login.Page1" 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="253" d:DesignWidth="276"
Title="Page1" Background="Blue">
<Grid>
<TextBlock Text="Prince jain" HorizontalAlignment="Left" VerticalAlignment="Top" Height="50" Width="100" Margin="48,0,0,0" FontSize="40"></TextBlock>
<TextBlock Height="50" FontSize="18" HorizontalAlignment="Left" Margin="6,141,0,0" Name="txtnavigatevalue" Text="Test Navigation" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="134,144,0,0" Name="txtnavigation" VerticalAlignment="Top" Width="130" />
<Button Content="Check" Height="23" HorizontalAlignment="Left" Margin="134,187,0,0" Name="buttoncheck" VerticalAlignment="Top" Width="75" Click="buttoncheck_Click" />
</Grid>
</Page>
<Page x:Class="Login.Page2"
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="300" d:DesignWidth="300"
Title="Page2" Background="BlueViolet">
<Grid>
<TextBlock Height="23" HorizontalAlignment="Left" Margin="46,94,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" Width="198" />
</Grid>
</Page>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
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 Login
{
/// <summary>
/// Interaction logic for Page1.xaml
/// </summary>
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
}
private void buttoncheck_Click(object sender, RoutedEventArgs e)
{
Page2 p2 = new Page2();
p2.textBlock1.Text = txtnavigation.Text;
//txtnavigatevalue.Text = "";
NavigationService.Navigate(p2);
}
}
}
private void buttoncheck_Click(object sender, RoutedEventArgs e)
{
Page2 p2 = new Page2();
p2.textBlock1.Text = txtnavigation.Text;
//txtnavigatevalue.Text = "";
NavigationService.Navigate(p2);
}
Related
EDIT : My program doesn't start after adding the Background, and an icon to the program I added:
Icon="/assets/icon.ico"
Background="/assets/background.svg">
I checked if I had made a mistake but in my code everything is OK finally according to the IDE and according to me the beginner
Class MainWindow.xaml.cs :
using Avalonia.Controls;
using Avalonia.Interactivity;
namespace AvaloApp;
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void startButton(object sender, RoutedEventArgs e) {
Button start = new Button();
}
private void stopButton(object sender, RoutedEventArgs e) {
Button stop = new Button();
}
private void hotkeyButton(object sender, RoutedEventArgs e) {
Button hotkey = new Button();
}
}
Class MainWindow.xaml :
<Window xmlns="https://github.com/avaloniaui"
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"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AvaloApp.MainWindow"
Title="AvaloApp"
Icon="/assets/icon.ico"
Background="/assets/background.svg">
<Grid>
<Button Grid.Column="1" Grid.Row="0" x:Name="start" Content="Start" Click="startButton" Margin="5" HorizontalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height="50"/>
<Button Grid.Column="2" Grid.Row="0" x:Name="stop" Content="Stop" Click="stopButton" Margin="280,5,5,5" HorizontalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height="50"/>
<Button Grid.Column="3" Grid.ColumnSpan="2" Grid.Row="1" x:Name="hotkey" Content="Hotkey" Click="hotkeyButton" Margin="5,140,5,5" HorizontalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Height="50"/>
</Grid>
</Window>
I tried to debug the program, it says that it does not find the icon, except that it is placed here in the project structure: https://i.imgur.com/Z7WTJmW.png (in the assets folder) and in my code I have specified the location
Even removing the / before assets still doesn't find the file, and surely prevents the project from running properly
The result is the same when I put the images like this: https://i.imgur.com/AdX5LWe.png
What I'm trying to do is I have a textbox, and a '-' and a '+' button, and on button click I need to decrease/increase it's value. I have close to 0 experience with WPF, we only learnt console apps in school and now we got this task and I have no idea why this code sample is not working:
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 sudokuGUI
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
int ertek = Convert.ToInt32(textBox1.Text);
if (ertek > 4)
{
textBox1.Text = $"{ertek--}";
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
int ertek = Convert.ToInt32(textBox1.Text);
if (ertek < 9)
{
textBox1.Text = $"{ertek++}";
}
}
}
}
<Window x:Class="sudokuGUI.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:sudokuGUI"
mc:Ignorable="d"
Title="Sudoku-ellenőrző" Height="210" Width="540">
<Grid>
<TextBox x:Name="textBox2" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="512" Margin="10,90,0,0"/>
<Label Content="Új feladvány mérete:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0"/>
<Button Content="-" HorizontalAlignment="Left" VerticalAlignment="Top" Width="20" Margin="144,13,0,0" Click="Button_Click"/>
<Button Content="+" HorizontalAlignment="Left" VerticalAlignment="Top" Width="20" Margin="194,13,0,0" Click="Button_Click_1"/>
<Label Content="Kezdőállapot:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,59,0,0"/>
<Label x:Name="label1" Content="Hossz: 0" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,118,0,0"/>
<Button Content="Ellenőrzés" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="447,118,0,0"/>
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="20" TextWrapping="Wrap" Text="4" VerticalAlignment="Top" Width="20" Margin="169,13,0,0" TextAlignment="Center" IsEnabled="False"/>
</Grid>
</Window>
Any help is appreciated!
ertek++ is a postfix increment operator. The result of ertek++ is the value of ertek before the operation.
use simple + operator:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
int ertek = Convert.ToInt32(textBox1.Text);
if (ertek < 9)
{
textBox1.Text = $"{ertek+1}";
}
}
i have an issue on Visual Studio(wpf) with the listbox.
If i want to insert or delete some data from the database, then they are working,but the listbox will just be refreshed if i'm clicking the menuitem for once. I think this is due to the load method, but why isn't it refreshing the data?
This is my XAML-Code:
<UserControl x:Class="WpfApplication1.AutorenBearbeiten"
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"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
d:DesignHeight="400" d:DesignWidth="300" Loaded="UserControl_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="28*" />
<ColumnDefinition Width="36*" />
</Grid.ColumnDefinitions>
<TextBlock Text="Medien" Grid.ColumnSpan="2"
FontSize="16" />
<ListBox x:Name="box" Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=at_nachname}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel DataContext="{Binding ElementName=box,Path=SelectedItem}" Grid.Column="1" Grid.Row="1" >
<TextBlock Text="Autoren_id" />
<TextBox Text="{Binding Path=at_id}" MaxLength="5"/>
<TextBlock Text="Vorname" />
<TextBox Text="{Binding Path=at_vorname}" MaxLength="30"/>
<TextBlock Text="Nachname" />
<TextBox Text="{Binding Path=at_nachname}" MaxLength="30"/>
<TextBlock Text="Geburtsdatum" />
<TextBox MaxLength="30" Text="{Binding Path=at_gebDatum, StringFormat=dd.MM.yyyy}" />
<Button Name="speichern" Height="23" Margin="4" Click="speichern_Click">Änderungen speichern</Button>
<Button Name="loeschen" Height="23" Margin="4" Click="loeschen_Click">Löschen</Button>
<StackPanel DataContext="{Binding ElementName=box}" Grid.Column="1" Grid.Row="1" >
<TextBlock Text="Autoren_id" />
<TextBox x:Name="id" MaxLength="5"/>
<TextBlock Text="Vorname" />
<TextBox x:Name="vorname" MaxLength="30"/>
<TextBlock Text="Nachname" />
<TextBox x:Name="nachname" MaxLength="30"/>
<TextBlock Text="Geburtsdatum" />
<TextBox x:Name="datum" MaxLength="30"/>
<Button x:Name="neubutton" Height="23" Margin="4" Click="neu_Click">Neu</Button>
<TextBlock Name="submitfehler" FontWeight="Bold" Foreground="Red" />
</StackPanel>
</StackPanel>
</Grid>
</UserControl>
And this is the xaml.cs file :
using System;
using System.Collections.Generic;
using System.Data.Entity;
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 WpfApplication1
{
/// <summary>
/// Interaction logic for AutorenBearbeiten.xaml
/// </summary>
public partial class AutorenBearbeiten : UserControl
{
libraryEntities6 db = new libraryEntities6();
public AutorenBearbeiten()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
var erg = db.a_autor;
erg.Load();
box.ItemsSource = erg.Local.OrderBy(m => m.at_id);
box.ItemsSource =
(from m in db.a_autor
orderby m.at_id
select m).ToList();
}
private void speichern_Click(object sender, RoutedEventArgs e)
{
try
{
db.SaveChanges();
}
catch(Exception e1)
{
submitfehler.Text = e1.Message;
}
}
private void loeschen_Click(object sender, RoutedEventArgs e)
{
a_autor am = (a_autor)box.SelectedItem;
if (am != null)
{
db.a_autor.Remove(am);
db.SaveChanges();
box.Items.Refresh();
}
}
private void neu_Click(object sender, RoutedEventArgs e)
{
a_autor autor = new a_autor();
autor.at_id = id.Text;
autor.at_vorname = vorname.Text;
autor.at_nachname = nachname.Text;
autor.at_gebDatum = Convert.ToDateTime(datum.Text);
//s1.s_k_klasse = liklassen.SelectedValue.ToString() setzt die Klasse via foreign key
//db.schuelers.AddObject(s1);
db.a_autor.Add(autor);
box.Items.Refresh();
/*
((klassen))liklassen.SelectedItem).schuelers.Add(s1); //setzt die klasse durch zuweisen zum nav.Property
lischueler.Items.Refresh(); //nötig weil das navigational seit ER 5 nicht observable ist
*/
}
}
}
A Picture from the window is below:
Window
There is no magic connection between the ListBox and the database so when you are calling the Add or Remove method of the DbContext the ListBox won't be affected.
What you should do is to set the ItemsSource property of the ListBox to an ObservableCollection<a_autor> and then call the Add/Remove method of this one besides calling the Add/Remove method of the DbContext:
System.Collections.ObjectModel.ObservableCollection<a_autor> _sourceCollection;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
var erg = db.a_autor;
erg.Load();
_sourceCollection = new System.Collections.ObjectModel.ObservableCollection<a_autor>((from m in db.a_autor
orderby m.at_id
select m).ToList());
box.ItemsSource = _sourceCollection;
}
private void loeschen_Click(object sender, RoutedEventArgs e)
{
a_autor am = (a_autor)box.SelectedItem;
if (am != null)
{
_sourceCollection.Remove(am);
db.a_autor.Remove(am);
db.SaveChanges();
box.Items.Refresh();
}
}
private void neu_Click(object sender, RoutedEventArgs e)
{
a_autor autor = new a_autor();
autor.at_id = id.Text;
autor.at_vorname = vorname.Text;
autor.at_nachname = nachname.Text;
autor.at_gebDatum = Convert.ToDateTime(datum.Text);
_sourceCollection.Add(autor);
db.a_autor.Add(autor);
box.Items.Refresh();
}
You can create an ObservableCollection instead of binding to the List. ObservableCollection implements INotifyPropertyChanged so it can send a notification whenever something is changed in the container.
Also, I would suggest you to try
public void RefreshListBox()
{
box.ItemsSource =
(from m in db.a_autor
orderby m.at_id
select m).ToList();
}
and call this after db.SaveChanges()
You should use MVVM pattern instead of code behind and use property changes. First result in Google:
https://www.codeproject.com/Articles/819294/WPF-MVVM-step-by-step-Basics-to-Advance-Level
I hope it helps you.
Juan
i have two user control and when i click on a button from the first user control ,another user control shows up :
The first user control is:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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 Navigateur.Presentation.UserControlWork
{
/// <summary>
/// Logique d'interaction pour ListeBlanche.xaml
/// </summary>
public partial class ListeBlanche : UserControl
{
public ListeBlanche()
{
InitializeComponent();
}
private async void Button_Click_1(object sender, RoutedEventArgs e)
{
ServiceReferenceParent.ParentServiceClient wcfParent = new ServiceReferenceParent.ParentServiceClient();
bool existe = await wcfParent.LogInAsync(textmail.Text, textpass.Text);
if (existe)
{
//grid.Children.Remove(this);
//this.Visibility = System.Windows.Visibility.Collapsed;
//ParentControl parmain = new ParentControl();
//parmain.Visibility = System.Windows.Visibility.Visible;
//parmain.
}
else
{
Popup1.IsOpen = true;
}
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
MainWindow main = new MainWindow();
ParentControl parmain = new ParentControl();
main.gridMain.Children.Add(parmain);
parmain.Visibility = System.Windows.Visibility.Visible;
this.Visibility = System.Windows.Visibility.Hidden;
}
}
}
and when i click on "Button_Click_2",ParentControl shows up but it never shows.
Fullscreen:
this is the ParenControl xaml code:
`<UserControl x:Class="Navigateur.Presentation.UserControlWork.ParentControl"
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" Width="586.194" Height="316.418">
<Grid Background="#FF65B4EC" ClipToBounds="True">
<Canvas HorizontalAlignment="Left" Height="69" VerticalAlignment="Top" Width="586" Background="#FF5ACAFF" ClipToBounds="True">
<Label Content="Création de votre compte parent" Canvas.Left="188" Canvas.Top="20" RenderTransformOrigin="0.5,0.5" Width="155" Background="#FF65B4EC" FontWeight="Black">
<Label.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="-0.376"/>
<TranslateTransform/>
</TransformGroup>
</Label.RenderTransform>
</Label>
</Canvas>
<Label Content="Nom Compte" HorizontalAlignment="Left" Margin="121,106,0,0" VerticalAlignment="Top"/>
<Label Content="Pseudo" HorizontalAlignment="Left" Margin="323,165,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.498,0.006"/>
<Label Content="Prénom" HorizontalAlignment="Left" Margin="121,219,0,0" VerticalAlignment="Top"/>
<Label Content="Nom" HorizontalAlignment="Left" Margin="323,214,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.2,0.471"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="121,132,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="335"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="323,191,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="133"/>
<RadioButton Content="Monsieur" HorizontalAlignment="Left" Margin="121,191,0,0" VerticalAlignment="Top" Height="23"/>
<RadioButton Content="Madame" HorizontalAlignment="Left" Margin="192,191,0,0" VerticalAlignment="Top" Height="23"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="121,245,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="131"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="323,245,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="133" RenderTransformOrigin="0.5,0.5"/>
<Button Content="Annuler" HorizontalAlignment="Left" Margin="192,288,0,0" VerticalAlignment="Top" Width="75" Height="23"/>
<Button Content="Suivant" HorizontalAlignment="Left" Margin="297,288,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</UserControl>`
and this is my main window xaml code:
<Window x:Name="wndmain" x:Class="Navigateur.Presentation.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:auth="clr-namespace:Navigateur.Presentation.UserControlWork"
Title="MainWindow" Height="317" Width="586.194" WindowState="Maximized" WindowStyle="None" Topmost="True" ResizeMode="NoResize">
<Grid x:Name="gridMain">
<auth:ListeBlanche ClickedInUserControl="OnClickedInUserControl"></auth:ListeBlanche>
</Grid>
</Window>
I think you need to use a RoutedEvent in your UserControl to get this to work.
So change your click event in your usercontrol to:
public event RoutedEventHandler ClickedInUserControl;
private void Button_Click_2(object sender, RoutedEventArgs e)
{
if (ClickedInUserControl != null)
{
ClickedInUserControl(this, new RoutedEventArgs());
}
}
Then in you MainWindow's xaml add this too the UserControl:
ClickedInUserControl="OnClickedInUserControl"
And then add the event handler on your mainwindow's codebehind
private void OnClickedInUserControl(object sender, RoutedEventArgs e)
{
ParentControl parmain = new ParentControl();
this.gridMain.Children.Add(parmain);
parmain.Visibility = System.Windows.Visibility.Visible;
// also remove the original usercontrol from the grid and collapse it's vilisibilty
}
Also, I would highly recommend that you look into using Commands rather than Events for this kind of thing, but I used events because that's what you were working with.
I am learning c# and I have been asked to work on a project using WPF, which I don't know much. We are using MUI as well.
I am trying to achieve a pretty basic task. I have two pages called ClientRNG.xamland ServerRNG.xaml. In ClientRNG.xaml I have two buttons and two textfield, when each button is pressed a random number is generated and it appears in a text box. In ServerRNG there are just one button and one textfield, with the same functionality as mentioned above.
So I'll end up with three different random numbers, one in ServerRNG.xaml and two in ClientRNG.
What i want to do is to pass these random numbers to another page called SSL.xaml.
The pages are created in MainWindow.xml:
<mui:ModernWindow.MenuLinkGroups>
<mui:LinkGroup DisplayName="network security">
<mui:LinkGroup.Links>
<mui:Link DisplayName="Home" Source="/Pages/Home.xaml" />
<mui:Link DisplayName="RNG" Source="/Pages/ClientRNG.xaml" />
<mui:Link DisplayName="3DES" Source="/Pages/3des.xaml" />
<mui:Link DisplayName="RSA" Source="/Pages/RSA.xaml" />
<mui:Link DisplayName="SHA-1" Source="/Pages/sha1.xaml" />
<mui:Link DisplayName="PKI Certificates" Source="/Pages/pki.xaml" />
<mui:Link DisplayName="SSL" Source="/Pages/SSL.xaml" />
</mui:LinkGroup.Links>
</mui:LinkGroup>
<mui:LinkGroup DisplayName="settings" GroupName="settings">
<mui:LinkGroup.Links>
<mui:Link DisplayName="software" Source="/Pages/Settings.xaml" />
</mui:LinkGroup.Links>
</mui:LinkGroup>
</mui:ModernWindow.MenuLinkGroups>
<mui:ModernWindow.TitleLinks>
<mui:Link DisplayName="settings" Source="/Pages/Settings.xaml" />
</mui:ModernWindow.TitleLinks>
Code in ClientRNG:
namespace NetworkSecuritySSLTest.Pages
{
/// <summary>
/// Interaction logic for RNG.xaml
/// </summary>
public partial class ClientRNG : UserControl
{
public ClientRNG()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Random r = new Random(1);
int number = r.Next(0, 100);
r1Out.Text = number.ToString();
SharingManager.GlobalValue = number;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Random r = new Random(3);
int number = r.Next(0, 100);
pmsOutC.Text = number.ToString();
}
here is the code I have in ServerRNG:
namespace NetworkSecuritySSLTest.Pages
{
/// <summary>
/// Interaction logic for RNG.xaml
/// </summary>
public partial class ServerRNG : UserControl
{
private SplitPage1 sp;
public ServerRNG()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Random r = new Random(2);
int number = r.Next(0, 100);
r2Out.Text = number.ToString();
SharingManager.GlobalValue = number;
}
}
}
and this is the code behind SSL class
namespace NetworkSecuritySSLTest.Pages
{
/// <summary>
/// Interaction logic for SplitPage1.xaml
/// </summary>
public partial class SplitPage1 : UserControl
{
private int r1FromClient;
public SplitPage1()
{
InitializeComponent();
SharingManager.ValueChanged += UpdateTextBox1;
SharingManager.ValueChanged += UpdateTextBox2;
}
public void UpdateTextBox1(object sender, NumericEventArgs e)
{
r1SSLBox.Text = e.Value.ToString(); // Update textBox
}
public void UpdateTextBox2(object sender, NumericEventArgs e)
{
r2SSLBox.Text = e.Value.ToString(); // Update textBox
}
}
}
here are the xaml:
'SplitPage1'
<UserControl x:Class="NetworkSecuritySSLTest.Pages.SplitPage1"
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"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
mc:Ignorable="d"
d:DesignWidth="766.507" Height="535">
<Grid Style="{StaticResource ContentRoot}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="6"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ScrollViewer>
<StackPanel>
<TextBlock Text="CLIENT" Style="{StaticResource Heading2}" />
<TextBlock x:Name="hello" Text="Hello Server. This is my Random Number and my Security Capabilities:" FontSize="12" FontStyle="Italic" Margin="0,10,0,0" UseLayoutRounding="False" TextWrapping="Wrap" />
<TextBlock x:Name="helloCont" Text="" FontSize="12" FontStyle="Italic" Margin="0,0,0,0" />
<TextBox x:Name ="r1SSLBox" HorizontalAlignment="Left" Height="57" Margin="10,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.498,0.404"/>
<TextBox x:Name ="r2SSLBox" HorizontalAlignment="Left" Height="57" Margin="10,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.498,0.404"/>
<TextBlock x:Name="VerifyDC" Text="I need to verify your Digital Certificate:" FontSize="12" FontStyle="Italic" Margin="0,10,0,0" />
<TextBlock x:Name="VerifyCont" Text="" FontSize="12" FontStyle="Italic" />
<TextBlock x:Name="MSK" Text="My Master Key is:" FontSize="12" FontStyle="Italic" Margin="0,10,0,0" />
<TextBlock x:Name="MSKCont" Text="" FontSize="12" FontStyle="Italic" />
</StackPanel>
</ScrollViewer>
<GridSplitter Grid.Column="1" />
<ScrollViewer Grid.Column="2 " Margin="{StaticResource SplitRight}">
<StackPanel>
<TextBlock Text="SERVER" Style="{StaticResource Heading2}" />
<TextBlock Text="Content goes here" />
</StackPanel>
</ScrollViewer>
<GridSplitter Grid.ColumnSpan="3" HorizontalAlignment="Left" Margin="0,23,0,0" VerticalAlignment="Top" Width="735"/>
<Button Content="Man-In-The-Middle-Attack" HorizontalAlignment="Left" VerticalAlignment="Top" Width="209" RenderTransformOrigin="0.055,0.397" Height="40" Margin="255,451,0,0" Grid.ColumnSpan="3" />
</Grid>
ClientRNG
<UserControl x:Class="NetworkSecuritySSLTest.Pages.ClientRNG"
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"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
mc:Ignorable="d"
d:DesignWidth="766.507" Height="535">
<Viewbox Stretch="None">
<Grid Style="{StaticResource ContentRoot}" Height="301" Margin="0" Width="435">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="0*"/>
</Grid.ColumnDefinitions>
<!-- TODO: set #SelectedSource -->
<mui:ModernTab Layout="Tab" Margin="0,52,0,0">
<mui:ModernTab.Links>
<!-- TODO: set #Source -->
<mui:Link DisplayName="Client" />
<mui:Link DisplayName="Server" Source="/Pages/ServerRNG.xaml" />
</mui:ModernTab.Links>
</mui:ModernTab>
<Button Content="GENERATE R# 1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.055,0.397" Height="26" Margin="10,52,0,0" FontSize="11" Click="Button_Click" />
<TextBox Name ="r1Out" HorizontalAlignment="Left" Height="57" Margin="10,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.498,0.404"/>
<Button Content="GENERATE MS" HorizontalAlignment="Left" VerticalAlignment="Top" Width="119" RenderTransformOrigin="0.055,0.397" Height="26" Margin="306,52,0,0" Click="Button_Click_2" />
<TextBox Name ="msOutC" HorizontalAlignment="Left" Height="57" Margin="306,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="119" RenderTransformOrigin="0.498,0.404"/>
<Button Content="GENERATE PMS" HorizontalAlignment="Left" VerticalAlignment="Top" Width="133" RenderTransformOrigin="0.055,0.397" Height="26" Margin="151,52,0,0" Click="Button_Click_1" />
<TextBox Name ="pmsOutC" HorizontalAlignment="Left" Height="57" Margin="151,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="133" RenderTransformOrigin="0.498,0.404"/>
<Label Content="Random Number Generator" HorizontalAlignment="Left" Height="19" Margin="10,10,0,0" VerticalAlignment="Top" Width="415" FontWeight="Bold"/>
</Grid>
</Viewbox>
and ServerRNG
<UserControl x:Class="NetworkSecuritySSLTest.Pages.ServerRNG"
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"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
mc:Ignorable="d"
d:DesignWidth="766.507" Height="535">
<Viewbox Stretch="None">
<Grid Style="{StaticResource ContentRoot}" Height="301" Margin="0" Width="435">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="0*"/>
</Grid.ColumnDefinitions>
<!-- TODO: set #SelectedSource -->
<mui:ModernTab Layout="Tab" Margin="0,52,0,0">
<mui:ModernTab.Links>
<!-- TODO: set #Source -->
<mui:Link DisplayName="Client" Source="/Pages/ClientRNG.xaml" />
<mui:Link DisplayName="Server" />
</mui:ModernTab.Links>
</mui:ModernTab>
<Button Name ="r2but" Content="GENERATE R# 2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.055,0.397" Height="26" Margin="76,52,0,0" FontSize="11" Click="Button_Click_1" />
<TextBox Name ="r2Out" HorizontalAlignment="Left" Height="57" Margin="76,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.498,0.404"/>
<Button Content="GENERATE MS" HorizontalAlignment="Left" VerticalAlignment="Top" Width="119" RenderTransformOrigin="0.055,0.397" Height="26" Margin="249,52,0,0" />
<TextBox Name ="msOutS" HorizontalAlignment="Left" Height="57" Margin="249,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="119" RenderTransformOrigin="0.498,0.404"/>
<Label Content="Random Number Generator" HorizontalAlignment="Left" Height="19" Margin="10,10,0,0" VerticalAlignment="Top" Width="415" FontWeight="Bold"/>
</Grid>
</Viewbox>
Now I was trying to use the solution posted by Omribitan but i am still struggling
According to what you said in the comment section that SplitPage1 is already shown,
What you are doing in your code is creating a new instance of SplitPage1 and passing it your data
SplitPage1 sp = new SplitPage1(); // This is a new page, not the one currently shown in your application
sp.Setr1SSLBox(number); // it should set the text box in SSL page
So if you want to set the text of the currently displayed SplitPage1, you need to get it's reference. It's hard to say how because I can't see your entire code but this is what I would consider:
Using IoC container to resolve the current instance of SplitPage1.
According to your code seems like there is a third party creating these pages. If that's true, it could pass ServerRNG a reference of the SplitPage1 it's creating which you'll be able to use later, for example :
public partial class ServerRNG : UserControl
{
private SplitPage1 sp;
public ServerRNG(SplitPage1 splitPage) : this()
{
sp = splitPage; // Save a reference to the currently displayed `SplitPage1` page
}
public ServerRNG()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Random r = new Random(2);
int number = r.Next(0, 100);
r2Out.Text = number.ToString();
sp.Setr1SSLBox(number); // Set the correct instance's text
}
}
Create a class which will expose a static property and event that will fire when that property changes:
public class SharingManager
{
// Define a global static event to be fired when the value is changing
public static event EventHandler<NumericEventArgs> ValueChanged;
public static int GlobalValue
{
set
{
// Fire ValueChanged event
if (ValueChanged != null)
ValueChanged(null, new NumericEventArgs(value));
}
}
}
public class NumericEventArgs : EventArgs
{
public NumericEventArgs(int value)
{
Value = value;
}
public int Value { get; set; }
}
Register a handler in SplitPage1
public SplitPage1()
{
InitializeComponent();
SharingManager.ValueChanged += UpdateTextBox;
}
public void UpdateTextBox(object sender, NumericEventArgs e)
{
r1SSLBox.Text = e.Value.ToString(); // Update textBox
}
In Button_Click_1 on ServerNRG, update the value to fire the event
Random r = new Random(2);
int number = r.Next(0, 100);
r2Out.Text = number.ToString();
SharingManager.GlobalValue = number;
Hope this helps
you should work on WPF using the MVVM design pattern.
Its kinda annoying without MVVM framework
I suggest you to use http://caliburnmicro.codeplex.com/
use this tutorial to get started
http://www.mindscapehq.com/blog/index.php/2012/01/12/caliburn-micro-part-1-getting-started/
it also explains how to use the mediator pattern (using caliburn micros event aggerator) to pass values/commands between diffrent windows.
http://www.mindscapehq.com/blog/index.php/2012/2/1/caliburn-micro-part-4-the-event-aggregator/