I'm trying to bind a variable to show in a label's content.
I use Galasoft's MVVM light to achieve this.
I send my viewmodel, my window and my window.cs. The issue is that nothing happens when I increase the values but something should.
Window: I have binding on only one label
<Window x:Class="DPCKOU_prog3hf_pong.Settings"
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:DPCKOU_prog3hf_pong"
mc:Ignorable="d"
Title="Settings" Height="406" Width="717"
Background="{StaticResource MainMenuBG}"
ResizeMode="NoResize"
WindowStartupLocation="CenterScreen"
>
<Grid>
<StackPanel>
<Label x:Name="Title" HorizontalContentAlignment="Center" HorizontalAlignment="Stretch"
Foreground="#00e4ff" FontSize ="28" Content="Size of pad"
Margin="10,30,10,10">
<Label.BitmapEffect>
<DropShadowBitmapEffect Color="#00e4ff" ShadowDepth="0" Opacity="1"/>
</Label.BitmapEffect>
</Label>
<Label x:Name="Size" HorizontalContentAlignment="Center" HorizontalAlignment="Stretch"
Foreground="#00e4ff" FontSize ="40" Content="{Binding Path=PadSize}"
Margin="10,0,10,10">
<Label.BitmapEffect>
<DropShadowBitmapEffect Color="#00e4ff" ShadowDepth="0" Opacity="1"/>
</Label.BitmapEffect>
</Label>
<DockPanel>
<Button x:Name="Increase" Content="Increase" HorizontalAlignment="Left"
Width="149" Height="46" Foreground="#00e4ff" Background="Black" Margin="120,10,10,10"
FontSize="18" BorderThickness="0" Click="Increase_Click">
<Button.BitmapEffect>
<DropShadowBitmapEffect Color="#00e4ff" ShadowDepth="0" Opacity="1"/>
</Button.BitmapEffect>
</Button>
<Button x:Name="Decrease" Content="Decrease" HorizontalAlignment="Right"
Width="149" Height="46" Foreground="#00e4ff" Background="Black" Margin="10,10,120,10"
FontSize="18" BorderThickness="0" Click="Decrease_Click">
<Button.BitmapEffect>
<DropShadowBitmapEffect Color="#00e4ff" ShadowDepth="0" Opacity="1"/>
</Button.BitmapEffect>
</Button>
</DockPanel>
<Button x:Name="Play" Content="Play" HorizontalAlignment="Center"
Width="149" Height="46" Foreground="#00e4ff" Background="Black" Margin="10,10,10,10"
FontSize="18" BorderThickness="0" Click="Play_Click">
<Button.BitmapEffect>
<DropShadowBitmapEffect Color="#00e4ff" ShadowDepth="0" Opacity="1"/>
</Button.BitmapEffect>
</Button>
<Button x:Name="Back" Content="Back" HorizontalAlignment="Center"
Width="149" Height="46" Foreground="#00e4ff" Background="Black" Margin="10,10,10,10"
FontSize="18" BorderThickness="0" Click="Back_Click">
<Button.BitmapEffect>
<DropShadowBitmapEffect Color="#00e4ff" ShadowDepth="0" Opacity="1"/>
</Button.BitmapEffect>
</Button>
</StackPanel>
</Grid>
The model's cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
namespace DPCKOU_prog3hf_pong
{
class SettingsVM : ViewModelBase
{
int padSize;
public int PadSize
{
get
{
return padSize;
}
set
{
if(value >=1 && value <= 6)
{
Set(ref padSize, value);
}
}
}
public SettingsVM()
{
padSize = 1;
}
}
}
and the window.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.Shapes;
using GalaSoft.MvvmLight;
namespace DPCKOU_prog3hf_pong
{
/// <summary>
/// Interaction logic for SingleSettings.xaml
/// </summary>
///
/*
* take it 100 pixels is the 4 units of the pad's size respectively.
* so 25 shall be 1 and that is the default value one can input.
* the player can input pad size from 1 to 6 meaning from 25 pixels to 150.
*/
public partial class Settings : Window
{
/*
* its sole purpose is to change the pad size.
*/
SettingsVM svm;
bool isPlaymodeSingle;
public Settings(bool isPlaymodeSingle)
{
InitializeComponent();
this.isPlaymodeSingle = isPlaymodeSingle;
svm = new SettingsVM();
}
private void Play_Click(object sender, RoutedEventArgs e)
{
if (isPlaymodeSingle)
{
//yes, solo
SinglePlayer spgame = new SinglePlayer();
spgame.Show();
Close();
/*
* show the game window and close this.
*/
}
else
{
//nope, multi.
}
}
private void Decrease_Click(object sender, RoutedEventArgs e)
{
svm.PadSize--;
}
private void Increase_Click(object sender, RoutedEventArgs e)
{
svm.PadSize++;
}
private void Back_Click(object sender, RoutedEventArgs e)
{
MainWindow mw = new MainWindow();
mw.Show();
Close();
}
}
}
You forgot to set the DataContext of the Window.
You can do that for example in the constructor of the Settings class:
public Settings(bool isPlaymodeSingle)
{
InitializeComponent();
this.isPlaymodeSingle = isPlaymodeSingle;
svm = new SettingsVM();
DataContext = svm; // here
}
Related
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 a main application at this there are border different buttons. This also includes the "Einstellung" button. Pressing this button should display a different content than before. This I realized with a UserControl.
Now you can make settings in this user control and save them.
This saving I made in the Settings
and with the Code
Vorschau.Properties.Settings.Default.BreiteBitmap = Int32.Parse(tbBreiteBitmap.Text);
Vorschau.Properties.Settings.Default.HoeheBitmap = Int32.Parse(tbHoeheBitmap.Text);
Vorschau.Properties.Settings.Default.AnzahlKoordinaten = Int32.Parse(tbAnzahlKoordinaten.Text);
Now I press the "Speichern" button. He saves me the settings.
I now change the UserControl, so I click on a button "Vorschau" and a new content is displayed to me. If I now click again on "Einstellung" is the content still there, as I have stored it.
But the problem comes. I close the application and I open it again. Now I click on the button "Einstellung" in the textboxes is only everywhere a 0 in it.
This should not be so, but the content should be from there in the past.
I retrieve this content when I start the application. See the code snippet.
public UCEinstellung()
{
InitializeComponent();
//Lade der Einstellungen
tbBreiteBitmap.Text = Vorschau.Properties.Settings.Default.BreiteBitmap.ToString();
tbHoeheBitmap.Text = Vorschau.Properties.Settings.Default.HoeheBitmap.ToString();
tbAnzahlKoordinaten.Text = Vorschau.Properties.Settings.Default.AnzahlKoordinaten.ToString();
}
AND
public MainWindow()
{
InitializeComponent();
MouseDown += Window_MouseDown;
einstellung.tbBreiteBitmap.Text = Vorschau.Properties.Settings.Default.BreiteBitmap.ToString();
einstellung.tbHoeheBitmap.Text = Vorschau.Properties.Settings.Default.HoeheBitmap.ToString();
einstellung.tbAnzahlKoordinaten.Text = Vorschau.Properties.Settings.Default.AnzahlKoordinaten.ToString();
}
Unfortunately this does not work.
I hope you can tell me where the error lies. In another application WITHOUT usercontrol works, this store and retrieve properly.
UCEinstellung
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;
using System.Diagnostics;
namespace Vorschau
{
/// <summary>
/// Interaktionslogik für UCEinstellung.xaml
/// </summary>
public partial class UCEinstellung : UserControl
{
public UCEinstellung()
{
InitializeComponent();
//Lade der Einstellungen
tbBreiteBitmap.Text = Vorschau.Properties.Settings.Default.BreiteBitmap.ToString();
tbHoeheBitmap.Text = Vorschau.Properties.Settings.Default.HoeheBitmap.ToString();
tbAnzahlKoordinaten.Text = Vorschau.Properties.Settings.Default.AnzahlKoordinaten.ToString();
}
private static Boolean kontrolliereObZahl(String pText)
{
int zahl1;
double zahl2;
Boolean istZahl;
if (Int32.TryParse(pText, out zahl1) || Double.TryParse(pText, out zahl2))
{
istZahl = true;
}
else
{
istZahl = false;
}
return istZahl;
}
private static Boolean textfeldRot(String pBreiteBitmap, String pHoeheBitmap, String pAnzahlKoordinaten)
{
Boolean istRot = true;
if (pBreiteBitmap.Equals("61381638") || pHoeheBitmap.Equals("61381638") || pAnzahlKoordinaten.Equals("61381638"))
{
istRot = true;
}
else
{
istRot = false;
}
return istRot;
}
private void btSpeichern_Click(object sender, RoutedEventArgs e)
{
if (textfeldRot(tbBreiteBitmap.BorderBrush.GetHashCode().ToString(), tbHoeheBitmap.BorderBrush.GetHashCode().ToString(), tbAnzahlKoordinaten.BorderBrush.GetHashCode().ToString()))
{
MessageBox.Show("Leider beinhalten die Eingabefelder falsche Werte.\n\nVersuchen Sie es bitte erneut.", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
else
{
try
{
Vorschau.Properties.Settings.Default.BreiteBitmap = Int32.Parse(tbBreiteBitmap.Text);
Vorschau.Properties.Settings.Default.HoeheBitmap = Int32.Parse(tbHoeheBitmap.Text);
Vorschau.Properties.Settings.Default.AnzahlKoordinaten = Int32.Parse(tbAnzahlKoordinaten.Text);
MessageBox.Show("Einstellungen wurden erfolgreich abgespeichert.", "Speicherung erfolgreich", MessageBoxButton.OK, MessageBoxImage.Asterisk);
}
catch (Exception error)
{
MessageBox.Show("Unglücklicherweise trat beim Speichervorgang ein Fehler auf.\n\nVersuchen Sie es bitte erneut.", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
Debug.WriteLine("Error - beim Speichervorgang " + error);
}
}
}
private void tbBreiteBitmap_LostFocus(object sender, RoutedEventArgs e)
{
try
{
if (kontrolliereObZahl(tbBreiteBitmap.Text) == false)
{
tbBreiteBitmap.BorderBrush = Brushes.Red;
}
else
{
tbBreiteBitmap.BorderBrush = Brushes.LightGray;
}
}
catch (Exception error)
{
Debug.WriteLine("Error 'tbBreitBitMap'" + error);
}
}
private void tbHoeheBitmap_LostFocus(object sender, RoutedEventArgs e)
{
try
{
if (kontrolliereObZahl(tbHoeheBitmap.Text) == false)
{
tbHoeheBitmap.BorderBrush = Brushes.Red;
}
else
{
tbHoeheBitmap.BorderBrush = Brushes.LightGray;
}
}
catch (Exception error)
{
Debug.WriteLine("Error 'tbHoeheBitmap'" + error);
}
}
private void tbAnzahlKoordinaten_LostFocus(object sender, RoutedEventArgs e)
{
try
{
if (kontrolliereObZahl(tbAnzahlKoordinaten.Text) == false)
{
tbAnzahlKoordinaten.BorderBrush = Brushes.Red;
}
else
{
tbAnzahlKoordinaten.BorderBrush = Brushes.LightGray;
}
}
catch (Exception error)
{
Debug.WriteLine("Error 'tbAnzahlKoordinaten'" + error);
}
}
}
}
<UserControl x:Class="Vorschau.UCEinstellung"
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:Vorschau"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Margin="0,0,-326,-161">
<TextBox x:Name="tbBreiteBitmap" HorizontalAlignment="Left" Height="23" Margin="145,38,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="85" LostFocus="tbBreiteBitmap_LostFocus"/>
<TextBox x:Name="tbHoeheBitmap" HorizontalAlignment="Left" Height="23" Margin="145,83,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="85" LostFocus="tbHoeheBitmap_LostFocus"/>
<Label x:Name="label" Content="Breite:" HorizontalAlignment="Left" Margin="10,36,0,0" VerticalAlignment="Top"/>
<Label x:Name="label_Copy" Content="Höhe:" HorizontalAlignment="Left" Margin="10,77,0,0" VerticalAlignment="Top"/>
<Label x:Name="label_Copy1" Content="in PX" HorizontalAlignment="Left" Margin="247,35,0,0" VerticalAlignment="Top"/>
<Label x:Name="label_Copy2" Content="in PX" HorizontalAlignment="Left" Margin="247,77,0,0" VerticalAlignment="Top"/>
<Label x:Name="label1" Content="einstellbare Größe des Bitmaps" HorizontalAlignment="Left" Margin="10,5,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
<Label x:Name="label1_Copy" Content="einstellbare Zahl der geenerierten Koordinaten" HorizontalAlignment="Left" Margin="10,172,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
<Label x:Name="label_Copy3" Content="Anzahl Koordinaten:" HorizontalAlignment="Left" Margin="10,203,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="tbAnzahlKoordinaten" HorizontalAlignment="Left" Height="23" Margin="145,203,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="85" LostFocus="tbAnzahlKoordinaten_LostFocus"/>
<Image x:Name="image" HorizontalAlignment="Left" Height="350" Margin="289,10,0,0" VerticalAlignment="Top" Width="350" Source="img/imageedit_1_9734874017.png" RenderTransformOrigin="0.5,0.5">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleY="1"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Image.RenderTransform>
</Image>
<Label x:Name="label1_Copy1" Content="Wertebreich des Koordinatensystems" HorizontalAlignment="Left" Margin="323,5,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
<Canvas HorizontalAlignment="Left" Height="405" Margin="299,23,0,0" VerticalAlignment="Top" Width="3" Background="#FFB8B8B8" RenderTransformOrigin="0.5,0.5">
<Canvas.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="-1"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Canvas.RenderTransform>
</Canvas>
<Button x:Name="btSpeichern" Content="Speichern" HorizontalAlignment="Left" Margin="30,418,0,0" VerticalAlignment="Top" Width="75" Click="btSpeichern_Click"/>
</Grid>
</UserControl>
MainWindow
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 Vorschau
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
UCEinstellung einstellung = new UCEinstellung();
UCVorschau vorschau = new UCVorschau();
public MainWindow()
{
InitializeComponent();
MouseDown += Window_MouseDown;
einstellung.tbBreiteBitmap.Text = Vorschau.Properties.Settings.Default.BreiteBitmap.ToString();
einstellung.tbHoeheBitmap.Text = Vorschau.Properties.Settings.Default.HoeheBitmap.ToString();
einstellung.tbAnzahlKoordinaten.Text = Vorschau.Properties.Settings.Default.AnzahlKoordinaten.ToString();
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
DragMove();
}
private void lbClose_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
this.Close();
}
private void lbMinimize_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
WindowState = WindowState.Minimized;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
lbWhereIAm.Content = "Vorschau";
conCon.Content = vorschau;
}
private void btEinstellung_Click(object sender, RoutedEventArgs e)
{
lbWhereIAm.Content = "Einstellung";
einstellung.tbBreiteBitmap.Text = "Test";
conCon.Content = einstellung;
einstellung.tbBreiteBitmap.Text = Vorschau.Properties.Settings.Default.BreiteBitmap.ToString();
einstellung.tbHoeheBitmap.Text = Vorschau.Properties.Settings.Default.HoeheBitmap.ToString();
einstellung.tbAnzahlKoordinaten.Text = Vorschau.Properties.Settings.Default.AnzahlKoordinaten.ToString();
}
}
}
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 Vorschau
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
UCEinstellung einstellung = new UCEinstellung();
UCVorschau vorschau = new UCVorschau();
public MainWindow()
{
InitializeComponent();
MouseDown += Window_MouseDown;
einstellung.tbBreiteBitmap.Text = Vorschau.Properties.Settings.Default.BreiteBitmap.ToString();
einstellung.tbHoeheBitmap.Text = Vorschau.Properties.Settings.Default.HoeheBitmap.ToString();
einstellung.tbAnzahlKoordinaten.Text = Vorschau.Properties.Settings.Default.AnzahlKoordinaten.ToString();
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
DragMove();
}
private void lbClose_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
this.Close();
}
private void lbMinimize_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
WindowState = WindowState.Minimized;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
lbWhereIAm.Content = "Vorschau";
conCon.Content = vorschau;
}
private void btEinstellung_Click(object sender, RoutedEventArgs e)
{
lbWhereIAm.Content = "Einstellung";
einstellung.tbBreiteBitmap.Text = "Test";
conCon.Content = einstellung;
einstellung.tbBreiteBitmap.Text = Vorschau.Properties.Settings.Default.BreiteBitmap.ToString();
einstellung.tbHoeheBitmap.Text = Vorschau.Properties.Settings.Default.HoeheBitmap.ToString();
einstellung.tbAnzahlKoordinaten.Text = Vorschau.Properties.Settings.Default.AnzahlKoordinaten.ToString();
}
}
}
<Window x:Name="windowsForm" x:Class="Vorschau.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:Vorschau"
mc:Ignorable="d"
Title="Vorschaukomponente" Height="514.583" Width="805.208" FontFamily="Century Gothic" WindowStartupLocation="Manual" BorderThickness="0" ResizeMode="NoResize" WindowStyle="None" Icon="img/coordinates.ico" Background="{x:Null}" AllowsTransparency="True">
<Window.Resources>
<Style x:Key="border_res" TargetType="{x:Type Border}">
<Setter Property="Background" Value="#3A3A3B" />
<Setter Property="CornerRadius" Value="10" />
</Style>
</Window.Resources>
<Border Style="{StaticResource border_res}">
<Grid>
<Canvas HorizontalAlignment="Left" Height="60" VerticalAlignment="Top" Width="185" Background="#FFE57E31">
<Canvas Height="64" Canvas.Top="451" Width="185" Background="#FF2C373F">
<Label x:Name="lbCopyright" Content="© Name 2017" Canvas.Left="10" Canvas.Top="29" Width="121" Foreground="#FF1B1D1F"/>
</Canvas>
<Canvas Height="391" Canvas.Top="60" Width="185" Background="#FF37424A">
<Button x:Name="btVorschau" Content="Vorschau" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="185" Height="50" Foreground="LightGray" FontSize="16"
HorizontalContentAlignment="Left" BorderBrush="{x:Null}" Click="Button_Click">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#FF37424A"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#FF303B43"/>
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<Button x:Name="btEinstellung" Content="Einstellung" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="185" Height="50" Foreground="LightGray" FontSize="16"
HorizontalContentAlignment="Left" BorderBrush="{x:Null}" Canvas.Top="50" Click="btEinstellung_Click">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#FF37424A"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#FF303B43"/>
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</Canvas>
<Canvas Height="60" Canvas.Left="185" Width="618" Background="#FFEEE9ED">
<Label x:Name="lbClose" Content="X" Canvas.Left="578" FontSize="20" MouseDoubleClick="lbClose_MouseDoubleClick"/>
<Label x:Name="lbMinimize" Content="-" Canvas.Left="556" FontSize="22" Canvas.Top="-2" MouseDoubleClick="lbMinimize_MouseDoubleClick"/>
<Label x:Name="lbWhereIAm" Content="Label" Canvas.Left="10" Canvas.Top="15" Width="162" FontSize="20"/>
</Canvas>
<Canvas x:Name="canvasContent" Height="455" Canvas.Left="185" Canvas.Top="60" Width="618" Background="#FFD1CFD0"/>
<Image x:Name="image" Height="38" Canvas.Left="10" Canvas.Top="10" Width="38" Source="img/coordinatesWhite.png"/>
<Label x:Name="lbLogoname" Content="Vorschaukomponente" Canvas.Left="37" Canvas.Top="10" Width="143" FontWeight="Bold" Foreground="White"/>
</Canvas>
<ContentControl x:Name="conCon" Content="ContentControl" Canvas.Left="86" Canvas.Top="31" Margin="185,60,0,0" Foreground="#FF002EFF"/>
</Grid>
</Border>
</Window>
It looks like you are just assigning the properties and not saving them. You need to call the Save method
Vorschau.Properties.Settings.Default.Save();
You can find the file under %userprofile%\appdata\local after saving. See here for details.
For this to work, the scope of the setting should be "User".
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'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);
}
I have a slider with minimum 0 and max 100.
I have a popup window that is visible when I mouseover the slider.
Currently, I binded the popup with the slider value.
What I want is:
When I move the mouse over the slider, I want to see value that suppose to be in that position.
For example:
My XAML:
<Window x:Class="WPFMavka.VideoPlayer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFMavka"
Title="MainWindow" WindowStyle="None" Width="1920" Height="1080" WindowState="Maximized" KeyboardNavigation.TabNavigation="None" Closing="Window_Closing">
<Grid Name="tst">
<local:MetroSlider x:Name="bla" MouseMove="Rectangle_MouseMove" MouseLeave="Rectangle_MouseLeave" IsMoveToPointEnabled="True" HorizontalAlignment="Left" Width="542" VerticalAlignment="Top" Margin="116,839,0,0" Value="0" Style="{DynamicResource SliderStyle1}" Height="66" AutoToolTipPrecision="0" AutoToolTipPlacement="TopLeft"/>
<Popup Name="floatingTip" AllowsTransparency="True" Placement="Relative" PlacementTarget="{Binding ElementName=bla}">
<Border BorderBrush="Black" BorderThickness="1" Padding="4">
<TextBlock Text="{Binding ElementName=bla, Path=Value}"></TextBlock>
</Border>
</Popup>
</Grid>
</Window>
My Code-Behind:
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.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WPFMavka
{
/// <summary>
/// Interaction logic for VideoPlayer.xaml
/// </summary>
public partial class VideoPlayer : Window
{
public VideoPlayer()
{
InitializeComponent();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Environment.Exit(1);
}
private void Rectangle_MouseMove(object sender, MouseEventArgs e)
{
if (!floatingTip.IsOpen) { floatingTip.IsOpen = true; }
Point currentPos = e.GetPosition(bla);
floatingTip.HorizontalOffset = currentPos.X-14;
floatingTip.VerticalOffset = -32;
}
private void Rectangle_MouseLeave(object sender, MouseEventArgs e)
{
floatingTip.IsOpen = false;
}
}
}
Found a better solution - changing the TextBlock onMouseOver the slide > I get the current position of mouse on the slider and then uses .ValueFromPoint()
XAML:
<Window x:Class="WPFMavka.VideoPlayer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFMavka"
Title="MainWindow" WindowStyle="None" Width="1920" Height="1080" WindowState="Maximized" KeyboardNavigation.TabNavigation="None" Closing="Window_Closing">
<Grid Name="tst">
<local:MetroSlider x:Name="slider" MouseMove="slider_MouseMove" MouseLeave="slider_MouseLeave" IsMoveToPointEnabled="True" HorizontalAlignment="Left" Width="746" VerticalAlignment="Top" Margin="330,851,0,0" Value="0" Style="{DynamicResource SliderStyle1}" Height="44"/>
<Popup Name="floatingTip" AllowsTransparency="True" Placement="Relative" PlacementTarget="{Binding ElementName=slider}">
<Border Name="floatingTipBorder" BorderBrush="Black" BorderThickness="1" Padding="4">
<TextBlock Name="sliderTextBlock"/>
</Border>
</Popup>
</Grid>
</Window>
Code-Behind:
private void slider_MouseMove(object sender, MouseEventArgs e)
{
if (!floatingTip.IsOpen) { floatingTip.IsOpen = true; }
Point currentPos = e.GetPosition(slider);
Track _track = slider.Template.FindName("PART_Track", slider) as Track;
sliderTextBlock.Text = _track.ValueFromPoint(currentPos).ToString();
floatingTip.HorizontalOffset = currentPos.X -(floatingTipBorder.ActualWidth / 2);
floatingTip.VerticalOffset = -32;
}
private void slider_MouseLeave(object sender, MouseEventArgs e)
{
floatingTip.IsOpen = false;
}
You can use AttachedCommands for this. You can download the library from here. So in this case you could do something like:
<Window x:Class="WPFMavka.VideoPlayer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFMavka"
xmlns:AttachedCommand="clr-namespace:AttachedCommandBehavior;assembly=AttachedCommandBehavior"
Title="MainWindow"
WindowStyle="None"
Width="1920"
Height="1080"
WindowState="Maximized"
KeyboardNavigation.TabNavigation="None"
Closing="Window_Closing">
<Grid Name="tst">
<local:MetroSlider x:Name="bla"
MouseMove="Rectangle_MouseMove"
MouseLeave="Rectangle_MouseLeave"
IsMoveToPointEnabled="True"
HorizontalAlignment="Left"
Width="542" VerticalAlignment="Top"
Margin="116,839,0,0"
Value="0"
Style="{DynamicResource SliderStyle1}"
Height="66" AutoToolTipPrecision="0"
AutoToolTipPlacement="TopLeft"
AttachedCommand:CommandBehavior.Event="MouseMove"
AttachedCommand:CommandBehavior.Command="{Binding UpdateCurrentMouseTimeCommand}"
AttachedCommand:CommandBehavior.CommandParameter="{Binding ElementName=Bla, Path=Value}"/>
<Popup Name="floatingTip" AllowsTransparency="True"
Placement="Relative"
PlacementTarget="{Binding ElementName=bla}">
<Border BorderBrush="Black" BorderThickness="1" Padding="4">
<TextBlock Text="{Binding CurrentMouseTime, NotifyOnSourceUpdated=True, Mode=OneWay}" />
</Border>
</Popup>
</Grid>
</Window>
Then in your command UpdateCurrentMouseTimeCommand you set the property
private string currentMouseTime = String.Empty;
public string CurrentMouseTime
{
get { return value; }
set
{
if (currentMouseTime == value)
return;
currentMouseTime = value;
OnPropertyChanged("CurrentMouseTime");
}
}
Where the containing class must implement INotifyPropertyChanged.
I hope this helps.