textbox text not becoming empty on button command - c#

I have a button with binding as ClearCommand which clears the value of text box but it is not working.
public SellerDetailsViewModel() //constructor
{
sdObject = new SellerDetailsTable();
_SaveCommand = new RelayCommand(Save, CanSave);
_ClearCommand = new RelayCommand(clear, Canclear);
}
private readonly ICommand _ClearCommand;
public ICommand ClearCommand { get { return _ClearCommand; } }
public event PropertyChangedEventHandler PropertyChanged;
public void onPropertyChange(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public bool Canclear(object obj)
{
return true;
}
public void clear(object obj)
{
this.PancardNumber = "";
this.ContactNumber = 0;
this.FirstName = "";
this.LastName = "";
this.MiddleName = "";
this.OtherDocument = "";
this.Address = "";
}
<Page x:Class="CarDealer.SellerDetails"
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="800" d:DesignWidth="900"
xmlns:ViewModels="clr-namespace:CarDealer.PresentationLayer.ViewModel"
Title="SellerDetails">
<Page.Resources>
<ViewModels:SellerDetailsViewModel x:Key="ViewModel">
</ViewModels:SellerDetailsViewModel>
<ControlTemplate x:Key="TextBoxErrorTemplate">
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Bottom" Foreground="Red" FontSize="10pt"
Text="{Binding ElementName=MyAdorner,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
<Border BorderBrush="Red" BorderThickness="2" Width="225" Height="35" >
<AdornedElementPlaceholder Name="MyAdorner" />
</Border>
</DockPanel>
</ControlTemplate>
</Page.Resources>
<Grid DataContext="{Binding Source={StaticResource ViewModel}}">
<Label Content="Seller Details" Height="39" HorizontalAlignment="Left" Margin="27,25,0,0" Name="lblTitle" VerticalAlignment="Top" FontSize="22" FontWeight="Bold" FontFamily="Times New Roman" Foreground="#FF1313D8" Width="169" />
<Label Content="First Name" Height="28" HorizontalAlignment="Left" Margin="44,87,0,0" Name="lblCustName" VerticalAlignment="Top" FontSize="16" />
<Label Content="Address" FontSize="16" Height="28" HorizontalAlignment="Left" Margin="461,253,0,0" Name="lblAdress" VerticalAlignment="Top" />
<Label Content="Pan Card Number" FontSize="16" Height="28" HorizontalAlignment="Left" Margin="44,243,0,0" Name="lblPanCardNumber" VerticalAlignment="Top" />
<Label Content="Contact Number" FontSize="16" Height="28" HorizontalAlignment="Left" Margin="44,320,0,0" Name="lblContactNumber" VerticalAlignment="Top" />
<Label Content="Handover Date" FontSize="16" Height="28" HorizontalAlignment="Left" Margin="461,170,0,0" Name="label1" VerticalAlignment="Top" />
<Label Content="Other Document" FontSize="16" Height="28" HorizontalAlignment="Left" Margin="44,399,0,0" Name="label2" VerticalAlignment="Top" />
<Button Content="Save" Height="37" Command="{Binding SaveCommand}" HorizontalAlignment="Left" Margin="299,543,0,0" Name="btnSave" VerticalAlignment="Top" Width="131" FontSize="22" />
<TextBox Height="33" Text="{Binding FirstName, Mode=TwoWay}" HorizontalAlignment="Left" Margin="205,92,0,0" Name="txtFirstName" VerticalAlignment="Top" Width="225" BorderThickness="2" FontSize="16" />
<Label Content="Last Name" FontSize="16" Height="28" HorizontalAlignment="Left" Margin="44,165,0,0" Name="label3" VerticalAlignment="Top" />
<TextBox BorderThickness="2" Text="{Binding LastName, Mode=TwoWay}" Height="33" HorizontalAlignment="Left" Margin="205,170,0,0" Name="txtLastName" VerticalAlignment="Top" Width="225" FontSize="16" />
<TextBox BorderThickness="2" Text="{Binding PancardNumber, Mode=TwoWay}" Height="33" HorizontalAlignment="Left" Margin="205,248,0,0" Name="txtPanCard" VerticalAlignment="Top" Width="225" FontSize="16" />
<TextBox BorderThickness="2" Height="33" HorizontalAlignment="Left" Margin="205,325,0,0" Validation.ErrorTemplate="{StaticResource TextBoxErrorTemplate}" Name="txtcontactNumber" VerticalAlignment="Top" Width="225" FontSize="16">
<TextBox.Text>
<Binding Path="ContactNumber">
<Binding.ValidationRules>
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<TextBox Height="126" Text="{Binding Address, Mode=TwoWay}" HorizontalAlignment="Left" Margin="627,248,0,0" Name="TxtAddress" VerticalAlignment="Top" Width="257" BorderThickness="2" FontSize="16" VerticalScrollBarVisibility="Visible" />
<TextBox BorderThickness="2" Text="{Binding OtherDocument, Mode=TwoWay}" Height="126" HorizontalAlignment="Left" Margin="205,399,0,0" Name="rchTxtOtherDoc" VerticalAlignment="Top" Width="257" FontSize="16" VerticalScrollBarVisibility="Visible" />
<DatePicker Height="33" Text="{Binding HandoverDate}" HorizontalAlignment="Left" Margin="627,170,0,0" Name="dtPickerHandoverDate" VerticalAlignment="Top" Width="225" FontSize="16" />
<Button Content="Clear All" Command="{Binding ClearCommand}" FontSize="22" Height="37" HorizontalAlignment="Left" Margin="500,543,0,0" Name="btnClearAll" VerticalAlignment="Top" Width="131" />
<Label Content="Middle Name" FontSize="16" Height="28" HorizontalAlignment="Left" Margin="471,87,0,0" Name="label4" VerticalAlignment="Top" />
<TextBox BorderThickness="2" Text="{Binding MiddleName, Mode=TwoWay}" FontSize="16" Height="33" HorizontalAlignment="Left" Margin="627,87,0,0" Name="txtMiddleName" VerticalAlignment="Top" Width="225" />
</Grid>
I have debugged the code execution. clear function is executed but in the view, the textbox is not empty. Can you help me figure out what I am doing wrong?
I have added remaining code please what is missing.

You should post the rest of your xaml. Assuming your controls are TextBox's...
public void clear(object obj)
{
this.PancardNumber.Text = "";
this.ContactNumber.Text = "0"; //depends what this control is
this.FirstName.Text = "";
this.LastName.Text = "";
this.MiddleName.Text = "";
this.OtherDocument.Text = "";
this.Address.Text = "";
}

Try this, hope it helps.
public void clear(object obj)
{
this.PancardNumber = "";
this.ContactNumber = 0;
this.FirstName = "";
this.LastName = "";
this.MiddleName = "";
this.OtherDocument = "";
this.Address = "";
// should work on other document
OnPropertyChanged("OtherDocument");
}
<Button Content="Clear All" Command="{Binding ClearCommand}" FontSize="22" Height="37" HorizontalAlignment="Left" Margin="500,543,0,0" Name="btnClearAll" VerticalAlignment="Top" Width="131" />
<TextBox BorderThickness="2" Text="{Binding OtherDocument, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="126" HorizontalAlignment="Left" Margin="205,399,0,0" Name="rchTxtOtherDoc" VerticalAlignment="Top" Width="257" FontSize="16" VerticalScrollBarVisibility="Visible" />

Related

Enabled View after login my application

I am a beginner in WPF /MVVM and I have a lot of problems..
One of theses, it that I've an application with a LoginView on the left and a content view on the left.
Theses 2 views are in the MainView like this :
First View "MainWindow"
<Window x:Class="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:iut1"
xmlns:ctrls="clr-namespace:iut1.Views"
mc:Ignorable="d"
Title="Outil de pilotage SCR" Height="650" Width="950"
WindowStartupLocation="CenterScreen"
DataContext="{Binding Path=MainWindowViewModel, Mode=OneWay, Source={StaticResource Locator}}">
<Grid>
<Viewbox Name="vbxConnexion" HorizontalAlignment="Left" Height="623" VerticalAlignment="Top" Width="224">
<ctrls:ConnexionView></ctrls:ConnexionView>
</Viewbox>
<Viewbox Name="vbxContenu" HorizontalAlignment="Left" Margin="233,0,0,0" Width="682" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Height="620" Stretch="None">
<ctrls:MigrationModeleView IsEnabled="{Binding EnabledView}" Height="623" Width="653" ></ctrls:MigrationModeleView>
</Viewbox>
</Grid>
Seconde View : "ConnexionView"
<UserControl x:Class="iut1.Views.ConnexionView"
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:iut1.Views"
xmlns:w="clr-namespace:iut1.Classes"
mc:Ignorable="d"
d:DesignHeight="720" d:DesignWidth="240"
DataContext="{Binding Path=MainWindowViewModel, Mode=OneWay, Source={StaticResource Locator}}">
<UserControl.Resources>
<w:EnumMatchToBooleanConverter x:Key="enumConverter" />
</UserControl.Resources>
<Grid Margin="10,30,-13,-18">
<GroupBox x:Name="gpbSource" HorizontalAlignment="Left" Margin="6,10,0,386" Width="220" Header="Environnement source" Background="{DynamicResource {x:Static SystemColors.ScrollBarBrushKey}}" >
<Grid Margin="0,0,0,20">
<!--<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="317" VerticalAlignment="Top" Width="220" RadiusY="5" RadiusX="5" Stroke="Black" Opacity="0.1" Margin="0,5,0,0"/>-->
<Label x:Name="lblIdentifiant" Content="Identifiant de l'utilisateur :" HorizontalAlignment="Left" Margin="4,24,0,0" VerticalAlignment="Top" Width="200"/>
<TextBox x:Name="txtNomUsager" Text="{Binding IdUtilisateur}" HorizontalAlignment="Left" Height="23" Margin="4,48,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" AutomationProperties.IsRequiredForForm="True" BorderThickness="2">
</TextBox>
<Label x:Name="Password" Content="Mot de passe :" HorizontalAlignment="Left" Margin="4,80,0,0" VerticalAlignment="Top" Width="200"/>
<PasswordBox x:Name="txtMotDePasse" w:PasswordHelper.Attach="True" w:PasswordHelper.Password="{Binding PasswordUtilisateur, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" Margin="4,103,0,0" VerticalAlignment="Top" Width="200"/>
<RadioButton x:Name="rdbUnit" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay,Converter={StaticResource enumConverter}, ConverterParameter=Unitaire}" GroupName="envSource" Width="79" Content="Unitaire" HorizontalAlignment="Left" Margin="4,160,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbFonctionnel" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay,Converter={StaticResource enumConverter}, ConverterParameter=Fonctionnel}" Content="Fonctionnel" GroupName="envSource" Width="79" HorizontalAlignment="Left" Margin="4,183,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbIntegre" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay,Converter={StaticResource enumConverter}, ConverterParameter=Integre}" GroupName="envSource" Width="79" Content="Intégré" HorizontalAlignment="Left" Margin="4,206,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbAcceptation" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay,Converter={StaticResource enumConverter}, ConverterParameter=Acceptation}" GroupName="envSource" Width="90" Content="Acceptation" HorizontalAlignment="Left" Margin="114,162,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbFormation" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay,Converter={StaticResource enumConverter}, ConverterParameter=Formation}" GroupName="envSource" Width="90" Content="Formation" HorizontalAlignment="Left" Margin="114,183,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbProduction" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay,Converter={StaticResource enumConverter}, ConverterParameter=Production}" GroupName="envSource" Width="90" Content="Production" HorizontalAlignment="Left" Margin="114,206,0,0" VerticalAlignment="Top"/>
<Button x:Name="Login" Content="Connexion" HorizontalAlignment="Left" Margin="4,252,0,0" VerticalAlignment="Top" Width="200" Command="{Binding ButtonCommand}"/>
</Grid>
</GroupBox>
<!--<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="317" VerticalAlignment="Top" Width="220" RadiusY="5" RadiusX="5" Stroke="Black" Opacity="0.1" Margin="0,350,0,-179"/>-->
<GroupBox Width="220" Header="Environnement source" Margin="6,338,17,57" IsEnabled="False" Background="{DynamicResource {x:Static SystemColors.ScrollBarBrushKey}}">
<Grid Margin="0,0,0,20">
<Label x:Name="lblIdentifiantCopie" Content="Identifiant de l'utilisateur :" HorizontalAlignment="Left" Margin="4,24,0,0" VerticalAlignment="Top" Width="200"/>
<TextBox x:Name="txtNomUsagerCopie" HorizontalAlignment="Left" Height="23" Margin="4,47,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/>
<Label x:Name="lblMotDePasseCopie" Content="Mot de passe :" HorizontalAlignment="Left" Margin="4,80,0,0" VerticalAlignment="Top" Width="200"/>
<PasswordBox x:Name="txtMotDePasseCopie" HorizontalAlignment="Left" Height="23" Margin="4,103,0,0" VerticalAlignment="Top" Width="200"/>
<RadioButton x:Name="rdbUnitCopie" Width="79" Content="Unitaire" HorizontalAlignment="Left" Margin="4,160,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbFonctionnelCopie" Width="79" Content="Fonctionnel" HorizontalAlignment="Left" Margin="4,183,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbIntegreCopie" Width="79" Content="Intégré" HorizontalAlignment="Left" Margin="4,206,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbAcceptationCopie" Width="90" Content="Acceptation" HorizontalAlignment="Left" Margin="114,162,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbFormationCopie" Width="90" Content="Formation" HorizontalAlignment="Left" Margin="114,183,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbProductionCopie" Width="90" Content="Production" HorizontalAlignment="Left" Margin="114,206,0,0" VerticalAlignment="Top"/>
<Button x:Name="btnAjout" Content="Ajouter dans l'environnement" HorizontalAlignment="Left" Margin="4,252,0,0" VerticalAlignment="Top" Width="200"/>
</Grid>
</GroupBox>
</Grid>
MigrationModelView
<UserControl x:Class="iut1.Views.MigrationModeleView"
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:iut1.Views"
mc:Ignorable="d" d:DesignWidth="630" Height="587"
xmlns:w="clr-namespace:iut1.Classes"
DataContext="{Binding Path=MainWindowViewModel, Source={StaticResource Locator}}">
<Grid Margin="15,22,0,0">
<GroupBox Header="Migration de modèle de lettre" Margin="6,10,0,32">
<Grid Margin="0,0,0,4">
<Label x:Name="lblChoixPPP" Content="PPP :" HorizontalAlignment="Left" Margin="19,13,0,0" VerticalAlignment="Top"/>
<ComboBox x:Name="cbxChoixPPP" HorizontalAlignment="Left" Margin="67,13,0,0" VerticalAlignment="Top" Width="120"/>
<DataGrid x:Name="dgModeles" HorizontalAlignment="Left" Margin="19,61,0,0" VerticalAlignment="Top" Height="162" Width="565">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header=""/>
<DataGridTextColumn Header="Numéro de référence" Width="150" />
<!--Binding="{Binding Nom}"-->
<DataGridTextColumn Header="Titre du modèle de lettre" />
<!--Binding="{Binding Prenom}"-->
</DataGrid.Columns>
</DataGrid>
<Label x:Name="lblRechercher" Content="Rechercher :" HorizontalAlignment="Left" Margin="19,239,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="91,241,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="291"/>
<Button x:Name="btnAfficherTout" Content="Afficher tout" HorizontalAlignment="Left" Margin="387,244,0,0" VerticalAlignment="Top" Width="96"/>
<Button x:Name="btnAfficherSelection" IsEnabled="False" Content="Afficher sélection" HorizontalAlignment="Left" Margin="488,244,0,0" VerticalAlignment="Top" Width="96"/>
<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="19,319,0,0" VerticalAlignment="Top" Height="162" Width="565">
<DataGrid.Columns>
<DataGridTextColumn Header="Résultat de la copie" Width="150" />
<DataGridTextColumn Header="" />
</DataGrid.Columns>
</DataGrid>
<ProgressBar HorizontalAlignment="Left" Height="20" Margin="22,509,0,-26" VerticalAlignment="Top" Width="464"/>
<Button x:Name="btnDetails" Height="20" IsEnabled="False" Content="Détails" HorizontalAlignment="Left" Margin="491,509,0,-26" VerticalAlignment="Top" Width="96"/>
</Grid>
</GroupBox>
</Grid>
I would like to enable=true my view after click on the Login button on my ConnexionView.
I tryed a lot of thing and nothing works.
MY VM Code :
using GalaSoft.MvvmLight.CommandWpf;
using iut1.Classes;
using iut1.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace iut1.ViewModels
{
/// <summary>
///
/// </summary>
public class MainWindowViewModel : BaseViewModel, INotifyPropertyChanged
{
#region Proprietes privées
private readonly DelegateCommand<string> _clickCommand;
private string _idUtilisateur;
private string _passwordUtilisateur;
private string _environnementSource;
#endregion Proprietes privées
#region proprietes publiques
public string IdUtilisateur
{
get { return _idUtilisateur; }
set
{
_idUtilisateur = value;
RaisePropertyChanged(nameof(IdUtilisateur));
}
}
public string PasswordUtilisateur
{
get { return _passwordUtilisateur; }
set
{
_passwordUtilisateur = value;
RaisePropertyChanged(nameof(PasswordUtilisateur));
}
}
public string EnvironnementSource
{
get { return _environnementSource; }
set
{
_environnementSource = value;
RaisePropertyChanged(nameof(EnvironnementSource));
}
}
#endregion proprietes publiques
private bool _enabledView;
public bool EnabledView
{
get { return _enabledView; }
set
{
if (_enabledView == value)
{
return;
}
_enabledView = value;
RaisePropertyChanged(nameof(EnabledView));
OnPropertyChanged(nameof(EnabledView));
}
}
public MainWindowViewModel()
{
//ButtonCommand = new Classes.RelayCommand(new Action<object> (ConnectionBase));
ButtonCommand = new Classes.RelayCommand(o => { ConnectionBase(); }, o => true);
//_clickCommand = new DelegateCommand<string>(
// (s) => { ConnectionBase(); /* perform some action */ }
// );
EnabledView =false;
}
public DelegateCommand<string> ButtonClickCommand
{
get { return _clickCommand; }
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private void ConnectionBase()
{
if (Validation.ValidationConnection(IdUtilisateur, PasswordUtilisateur, EnvironnementSource))
{
EnabledView = true;
OnPropertyChanged(nameof(EnabledView));
}
else
{
string msgErreur = "KO";
}
}
private void Login(object parameter)
{
//return Validation.ValidationConnection(IdUtilisateur, password, "ENV");
}
private ICommand _buttonCommand;
public ICommand ButtonCommand
{
get
{
return _buttonCommand;
}
set
{
_buttonCommand = value;
}
}
}
}
The problem is that my view nerver become enable and I'm becoming crazy about that.
Thank you in advance for the help.
It is important that you raise the INotifyPropertyChanged event once, and in the correct place, so that all 'subscribers' are aware of the old value and the change to the new value. Because you are raising the event multiple times, I suspect that the 'subscribers' are basically being told that the value has been updated - old value: false, new value: false.
Without seeing the entirety of your solution and latest code, it is tough to accurately diagnose the problem, here is code which I have just refactored from what you have in your original post, clicking the button calls the ButtonCommand which immediately sets the EnabledView property to the opposite of what it currently is, this enables and disables the MigrationModeleView for me.
App.xaml
<Application x:Class="MvvmLight1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:MvvmLight1.ViewModel"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ignore="http://www.galasoft.ch/ignore"
StartupUri="MainWindow.xaml"
mc:Ignorable="d ignore">
<Application.Resources>
<!--Global View Model Locator-->
<vm:ViewModelLocator x:Key="Locator"
d:IsDataSource="True" />
</Application.Resources>
</Application>
MainWindow
<Window x:Class="MvvmLight1.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:ignore="http://www.galasoft.ch/ignore"
xmlns:mvvmLight1="clr-namespace:MvvmLight1"
mc:Ignorable="d ignore"
Height="650"
Width="950"
Title="MVVM Light Application"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Viewbox Name="vbxConnexion" HorizontalAlignment="Left" Height="623" VerticalAlignment="Top" Width="224">
<mvvmLight1:ConnexionView></mvvmLight1:ConnexionView>
</Viewbox>
<Viewbox Name="vbxContenu" HorizontalAlignment="Left" Margin="233,0,0,0" Width="682" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Height="620" Stretch="None">
<mvvmLight1:MigrationModeleView IsEnabled="{Binding EnabledView}" Height="623" Width="653" ></mvvmLight1:MigrationModeleView>
</Viewbox>
</Grid>
</Window>
ConnexionView.xaml
<UserControl x:Class="MvvmLight1.ConnexionView"
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:MvvmLight1"
mc:Ignorable="d"
d:DesignHeight="720" d:DesignWidth="240"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Grid Margin="10,30,-13,-18">
<GroupBox x:Name="gpbSource" HorizontalAlignment="Left" Margin="6,10,0,386" Width="220" Header="Environnement source" Background="{DynamicResource {x:Static SystemColors.ScrollBarBrushKey}}" >
<Grid Margin="0,0,0,20">
<!--<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="317" VerticalAlignment="Top" Width="220" RadiusY="5" RadiusX="5" Stroke="Black" Opacity="0.1" Margin="0,5,0,0"/>-->
<Label x:Name="lblIdentifiant" Content="Identifiant de l'utilisateur :" HorizontalAlignment="Left" Margin="4,24,0,0" VerticalAlignment="Top" Width="200"/>
<TextBox x:Name="txtNomUsager" Text="{Binding IdUtilisateur}" HorizontalAlignment="Left" Height="23" Margin="4,48,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" AutomationProperties.IsRequiredForForm="True" BorderThickness="2">
</TextBox>
<Label x:Name="Password" Content="Mot de passe :" HorizontalAlignment="Left" Margin="4,80,0,0" VerticalAlignment="Top" Width="200"/>
<PasswordBox x:Name="txtMotDePasse" HorizontalAlignment="Left" Height="23" Margin="4,103,0,0" VerticalAlignment="Top" Width="200"/>
<RadioButton x:Name="rdbUnit" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay, ConverterParameter=Unitaire}" GroupName="envSource" Width="79" Content="Unitaire" HorizontalAlignment="Left" Margin="4,160,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbFonctionnel" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay, ConverterParameter=Fonctionnel}" Content="Fonctionnel" GroupName="envSource" Width="79" HorizontalAlignment="Left" Margin="4,183,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbIntegre" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay, ConverterParameter=Integre}" GroupName="envSource" Width="79" Content="Intégré" HorizontalAlignment="Left" Margin="4,206,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbAcceptation" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay, ConverterParameter=Acceptation}" GroupName="envSource" Width="90" Content="Acceptation" HorizontalAlignment="Left" Margin="114,162,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbFormation" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay, ConverterParameter=Formation}" GroupName="envSource" Width="90" Content="Formation" HorizontalAlignment="Left" Margin="114,183,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbProduction" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay, ConverterParameter=Production}" GroupName="envSource" Width="90" Content="Production" HorizontalAlignment="Left" Margin="114,206,0,0" VerticalAlignment="Top"/>
<Button x:Name="Login" Content="Connexion" HorizontalAlignment="Left" Margin="4,252,0,0" VerticalAlignment="Top" Width="200" Command="{Binding ButtonCommand}"/>
</Grid>
</GroupBox>
<!--<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="317" VerticalAlignment="Top" Width="220" RadiusY="5" RadiusX="5" Stroke="Black" Opacity="0.1" Margin="0,350,0,-179"/>-->
<GroupBox Width="220" Header="Environnement source" Margin="6,338,17,57" IsEnabled="False" Background="{DynamicResource {x:Static SystemColors.ScrollBarBrushKey}}">
<Grid Margin="0,0,0,20">
<Label x:Name="lblIdentifiantCopie" Content="Identifiant de l'utilisateur :" HorizontalAlignment="Left" Margin="4,24,0,0" VerticalAlignment="Top" Width="200"/>
<TextBox x:Name="txtNomUsagerCopie" HorizontalAlignment="Left" Height="23" Margin="4,47,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/>
<Label x:Name="lblMotDePasseCopie" Content="Mot de passe :" HorizontalAlignment="Left" Margin="4,80,0,0" VerticalAlignment="Top" Width="200"/>
<PasswordBox x:Name="txtMotDePasseCopie" HorizontalAlignment="Left" Height="23" Margin="4,103,0,0" VerticalAlignment="Top" Width="200"/>
<RadioButton x:Name="rdbUnitCopie" Width="79" Content="Unitaire" HorizontalAlignment="Left" Margin="4,160,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbFonctionnelCopie" Width="79" Content="Fonctionnel" HorizontalAlignment="Left" Margin="4,183,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbIntegreCopie" Width="79" Content="Intégré" HorizontalAlignment="Left" Margin="4,206,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbAcceptationCopie" Width="90" Content="Acceptation" HorizontalAlignment="Left" Margin="114,162,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbFormationCopie" Width="90" Content="Formation" HorizontalAlignment="Left" Margin="114,183,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rdbProductionCopie" Width="90" Content="Production" HorizontalAlignment="Left" Margin="114,206,0,0" VerticalAlignment="Top"/>
<Button x:Name="btnAjout" Content="Ajouter dans l'environnement" HorizontalAlignment="Left" Margin="4,252,0,0" VerticalAlignment="Top" Width="200"/>
</Grid>
</GroupBox>
</Grid>
</UserControl>
MigrationModeleView.xaml
<UserControl x:Class="MvvmLight1.MigrationModeleView"
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:MvvmLight1"
mc:Ignorable="d" d:DesignWidth="630" Height="587"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Grid Margin="15,22,0,0">
<GroupBox Header="Migration de modèle de lettre" Margin="6,10,0,32">
<Grid Margin="0,0,0,4">
<Label x:Name="lblChoixPPP" Content="PPP :" HorizontalAlignment="Left" Margin="19,13,0,0" VerticalAlignment="Top"/>
<ComboBox x:Name="cbxChoixPPP" HorizontalAlignment="Left" Margin="67,13,0,0" VerticalAlignment="Top" Width="120"/>
<DataGrid x:Name="dgModeles" HorizontalAlignment="Left" Margin="19,61,0,0" VerticalAlignment="Top" Height="162" Width="565">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header=""/>
<DataGridTextColumn Header="Numéro de référence" Width="150" />
<!--Binding="{Binding Nom}"-->
<DataGridTextColumn Header="Titre du modèle de lettre" />
<!--Binding="{Binding Prenom}"-->
</DataGrid.Columns>
</DataGrid>
<Label x:Name="lblRechercher" Content="Rechercher :" HorizontalAlignment="Left" Margin="19,239,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="91,241,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="291"/>
<Button x:Name="btnAfficherTout" Content="Afficher tout" HorizontalAlignment="Left" Margin="387,244,0,0" VerticalAlignment="Top" Width="96"/>
<Button x:Name="btnAfficherSelection" IsEnabled="False" Content="Afficher sélection" HorizontalAlignment="Left" Margin="488,244,0,0" VerticalAlignment="Top" Width="96"/>
<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="19,319,0,0" VerticalAlignment="Top" Height="162" Width="565">
<DataGrid.Columns>
<DataGridTextColumn Header="Résultat de la copie" Width="150" />
<DataGridTextColumn Header="" />
</DataGrid.Columns>
</DataGrid>
<ProgressBar HorizontalAlignment="Left" Height="20" Margin="22,509,0,-26" VerticalAlignment="Top" Width="464"/>
<Button x:Name="btnDetails" Height="20" IsEnabled="False" Content="Détails" HorizontalAlignment="Left" Margin="491,509,0,-26" VerticalAlignment="Top" Width="96"/>
</Grid>
</GroupBox>
</Grid>
</UserControl>
MainViewModel.cs
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
namespace MvvmLight1.ViewModel
{
/// <summary>
/// This class contains properties that the main View can data bind to.
/// <para>
/// See http://www.mvvmlight.net
/// </para>
/// </summary>
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
}
public RelayCommand ButtonCommand
{
get { return new RelayCommand(() => { EnabledView = !EnabledView; }); }
}
private bool _environmentSource;
public bool EnvironnementSource
{
get { return _environmentSource; }
set { Set(ref _environmentSource, value); }
}
private string _idUtilisateur;
public string IdUtilisateur
{
get { return _idUtilisateur; }
set { Set(ref _idUtilisateur, value); }
}
private bool _enabledView;
public bool EnabledView
{
get { return _enabledView; }
set { Set(ref _enabledView, value); }
}
}
}
ViewModelLocator.cs
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;
namespace MvvmLight1.ViewModel
{
/// <summary>
/// This class contains static references to all the view models in the
/// application and provides an entry point for the bindings.
/// <para>
/// See http://www.mvvmlight.net
/// </para>
/// </summary>
public class ViewModelLocator
{
static ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register<MainViewModel>();
}
/// <summary>
/// Gets the Main property.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
"CA1822:MarkMembersAsStatic",
Justification = "This non-static member is needed for data binding purposes.")]
public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
/// <summary>
/// Cleans up all the resources.
/// </summary>
public static void Cleanup()
{
}
}
}

Programmaticallly generate grids in Stack Panel (C# UWP)

I have code to download data from WordPress
Here it is:
private async void Down_at_Start() {
RestAPI rest = new RestAPI("http://simplegames.com.ua/wp-json/wc/v1/", "ck_9d64c027d2c5f81b8bed3342eeccc6d337be813d", "cs_60697b1e6cbdeb8d62d19e0765e339f8e3334754");
WCObject wc = new WCObject(rest);
//Get all products
var orders = await wc.GetOrders();
string date = orders[0].date_created + "+0:00";
DateTime dt = DateTime.Parse(date);
string convertedDate = dt.ToString("dd/MM/yyyy HH:mm:ss");
try
{
date1.Text = convertedDate;
adress1.Text = orders[0].shipping.address_1.ToString() + " " + orders[0].shipping.address_2;
name1.Text = orders[0].billing.first_name.ToString();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
Debug.WriteLine(e.StackTrace);
}
string date_2 = orders[1].date_created + "+0:00";
DateTime dt2 = DateTime.Parse(date_2);
string convertedDate2 = dt2.ToString("dd/MM/yyyy HH:mm:ss");
try
{
date2.Text = convertedDate2;
adress2.Text = orders[1].shipping.address_1.ToString() + " " + orders[1].shipping.address_2;
name2.Text = orders[1].billing.first_name.ToString();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
Debug.WriteLine(e.StackTrace);
}
string date_3 = orders[2].date_created + "+0:00";
DateTime dt3 = DateTime.Parse(date_3);
string convertedDate3 = dt3.ToString("dd/MM/yyyy HH:mm:ss");
try
{
date3.Text = convertedDate3;
adress3.Text = orders[2].shipping.address_1.ToString() + " " + orders[2].shipping.address_2;
name3.Text = orders[2].billing.first_name.ToString();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
Debug.WriteLine(e.StackTrace);
}
string date_4 = orders[3].date_created + "+0:00";
DateTime dt4 = DateTime.Parse(date_4);
string convertedDate4 = dt4.ToString("dd/MM/yyyy HH:mm:ss");
try
{
date4.Text = convertedDate4;
adress4.Text = orders[3].shipping.address_1.ToString() + " " + orders[3].shipping.address_2;
name4.Text = orders[3].billing.first_name.ToString();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
Debug.WriteLine(e.StackTrace);
}
string date_5 = orders[4].date_created + "+0:00";
DateTime dt5 = DateTime.Parse(date_5);
string convertedDate5 = dt5.ToString("dd/MM/yyyy HH:mm:ss");
try
{
date5.Text = convertedDate5;
adress5.Text = orders[4].shipping.address_1.ToString() + " " + orders[4].shipping.address_2;
name5.Text = orders[4].billing.first_name.ToString();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
Debug.WriteLine(e.StackTrace);
}
}
As you see I have static number of fields.
My XAML:
<Page
x:Class="Milano.InWork"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Milano"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid BorderBrush="White" BorderThickness="1">
<Grid.Background>
<ImageBrush Stretch="Fill" ImageSource="Images/Background.png"/>
</Grid.Background>
<Grid HorizontalAlignment="Left" Height="720" VerticalAlignment="Top" Width="60" BorderBrush="#FFF5F1F1" BorderThickness="0,0,1,0">
<Button x:Name="MenuButton" Content="" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="38" Width="38">
<Button.Background>
<ImageBrush Stretch="Uniform" ImageSource="Images/Menu-100.png"/>
</Button.Background>
</Button>
<Button x:Name="logoutbutton" Content="" HorizontalAlignment="Left" Margin="10,650,0,0" VerticalAlignment="Top" Height="43" Width="38">
<Button.Background>
<ImageBrush Stretch="Uniform" ImageSource="Images/Logout_Button.png"/>
</Button.Background>
</Button>
</Grid>
<Grid HorizontalAlignment="Left" Height="47" Margin="63,2,-121,0" VerticalAlignment="Top" Width="1338" BorderBrush="#FFFDFDFD" Padding="0,0,0,1" BorderThickness="0,0,0,1">
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" TextWrapping="Wrap" Text="В Работе" VerticalAlignment="Top" Height="37" Width="1218" FontSize="32" FontFamily="SF UI Display" Padding="550,0,0,0" Foreground="White"/>
</Grid>
<ScrollViewer HorizontalAlignment="Left" Height="668" Margin="63,52,0,0" VerticalAlignment="Top" Width="350">
<StackPanel Height="1020" Width="350" BorderBrush="#FFFDFCFC" BorderThickness="0,0,1,0">
<Grid Height="204" BorderBrush="#FFFBF8F8" BorderThickness="0,0,0,1">
<TextBlock x:Name="date1" HorizontalAlignment="Left" TextAlignment="Center" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="350" Height="50" FontFamily="SF UI Display" FontSize="30" FontWeight="Light" Foreground="White" />
<TextBlock x:Name="adress1" TextAlignment="Center" HorizontalAlignment="Left" Margin="0,146,-1,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="58" Width="350" FontFamily="SF UI Display" FontSize="20" FontWeight="Light" Foreground="White" />
<TextBlock x:Name="name1" HorizontalAlignment="Left" TextAlignment="Center" Margin="0,86,-1,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="60" Width="350" FontFamily="SF UI Display" FontSize="30" FontWeight="Light" Foreground="White" Padding="0,0,0,0"/>
</Grid>
<Grid Height="204" BorderBrush="#FFFBF8F8" BorderThickness="0,0,0,1">
<TextBlock x:Name="date2" TextAlignment="Center" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="350" Height="50" Foreground="White" FontFamily="SF UI Display" FontSize="30" FontWeight="Light" />
<TextBlock x:Name="adress2" TextAlignment="Center" HorizontalAlignment="Left" Margin="0,145,-1,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="59" Width="350" FontFamily="SF UI Display" FontSize="20" FontWeight="Light" Foreground="White" />
<TextBlock x:Name="name2" HorizontalAlignment="Left" Margin="0,87,-1,0" TextAlignment="Center" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="64" Width="350" FontFamily="SF UI Display" FontSize="30" FontWeight="Light" Foreground="White" Padding="0,0,0,50"/>
</Grid>
<Grid Height="204" BorderBrush="#FFFBF8F8" BorderThickness="0,0,0,1">
<TextBlock x:Name="date3" HorizontalAlignment="Left" TextAlignment="Center" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="350" Height="50" Foreground="White" FontFamily="SF UI Display" FontSize="30" FontWeight="Light" />
<TextBlock x:Name="adress3" HorizontalAlignment="Left" TextAlignment="Center" Margin="0,143,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="350" Height="61" FontFamily="SF UI Display" FontSize="20" FontWeight="Light" Foreground="White" />
<TextBlock x:Name="name3" HorizontalAlignment="Left" TextAlignment="Center" Margin="0,86,-1,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="60" Width="350" FontFamily="SF UI Display" FontSize="30" FontWeight="Light" Foreground="White" Padding="0,0,0,50"/>
</Grid>
<Grid Height="204" BorderBrush="#FFFBF8F8" BorderThickness="0,0,0,1">
<TextBlock x:Name="date4" HorizontalAlignment="Left" TextWrapping="Wrap" TextAlignment="Center" Text="" VerticalAlignment="Top" Width="350" Height="50" Foreground="White" FontFamily="SF UI Display" FontSize="30" FontWeight="Light" />
<TextBlock x:Name="adress4" HorizontalAlignment="Left" Margin="0,153,0,0" TextWrapping="Wrap" TextAlignment="Center" Text="TextBlock" VerticalAlignment="Top" Height="61" Width="342" FontFamily="SF UI Display" FontSize="20" FontWeight="Light" Foreground="White" />
<TextBlock x:Name="name4" HorizontalAlignment="Left" TextAlignment="Center" Margin="0,86,-1,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="60" Width="350" FontFamily="SF UI Display" FontSize="30" FontWeight="Light" Foreground="White" Padding="0,0,0,50"/>
</Grid>
<Grid Height="204" BorderBrush="#FFFBF8F8" BorderThickness="0,0,0,1">
<TextBlock x:Name="date5" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" TextAlignment="Center" Width="350" Height="50" Foreground="White" FontFamily="SF UI Display" FontSize="30" FontWeight="Light" />
<TextBlock x:Name="adress5" TextAlignment="Center" HorizontalAlignment="Left" Margin="0,155,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="49" Width="349" FontFamily="SF UI Display" FontSize="20" FontWeight="Light" Foreground="White" />
<TextBlock x:Name="name5" HorizontalAlignment="Left" TextAlignment="Center" Margin="0,86,-1,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="60" Width="350" FontFamily="SF UI Display" FontSize="30" FontWeight="Light" Foreground="White" Padding="0,0,0,50"/>
</Grid>
</StackPanel>
</ScrollViewer>
</Grid>
How I can count how many orders in answer from website and generate grids?
Thank's for help so much! :)

C# XAML - Getting the XAML Content property (contentControl.Content)

So I have my AML keypad as such :
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBox Name="btnTotal" Width="280" Height="60" Grid.ColumnSpan="3" Grid.Row="0" Margin="10,10,10,10" Background="#302F37" Foreground="AntiqueWhite" FontSize="35"></TextBox>
<Button x:Name="btnZzero" Content="0" Width="80" Height="60" Grid.Column="0" Grid.Row="4" Margin="5,5,5,5" Background="#302F37" Foreground="White" Focusable="False" Click="btnZzero_Click"></Button>
<Button x:Name="btnOk" Content="OK" Width="80" Height="60" Grid.Column="1" Grid.Row="4" Margin="5,5,5,5" Click="btnOk_Click" Background="#FF8FC377" Focusable="False"></Button>
<Button x:Name="btnCancel" Content="Cancel" Width="80" Height="60" Grid.Column="2" Grid.Row="4" Margin="5,5,5,5" Click="cancel_Click" BorderBrush="Black" Background="#FFD64D4D" Focusable="False"></Button>
<Button x:Name="btnOne" Content="1" Width="80" Height="60" Grid.Column="0" Grid.Row="3" Margin="14,6,0,6" Focusable="False" Background="#302F37" Foreground="White" HorizontalAlignment="Left" Click="btnOne_Click"></Button>
<Button x:Name="btnTwo" Content="2" Width="80" Height="60" Grid.Column="1" Grid.Row="3" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnTwo_Click"/>
<Button x:Name="btnThree" Content="3" Width="80" Height="60" Grid.Column="2" Grid.Row="3" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnThree_Click"/>
<Button x:Name="btnFour" Content="4" Width="80" Height="60" Grid.Column="0" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnFour_Click"></Button>
<Button x:Name="btnFive" Content="5" Width="80" Height="60" Grid.Column="1" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnFive_Click"></Button>
<Button x:Name="btnSix" Content="6" Width="80" Height="60" Grid.Column="2" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnSix_Click"></Button>
<Button x:Name="btnSeven" Content="7" Width="80" Height="60" Grid.Column="0" Grid.Row="1" Margin="12,5,9,6" Focusable="False" Background="#302F37" Foreground="White" Click="btnSeven_Click"></Button>
<Button x:Name="btnEight" Content="8" Width="80" Height="60" Grid.Column="1" Grid.Row="1" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnEight_Click"></Button>
<Button x:Name="btnNine" Content="9" Width="80" Height="60" Grid.Column="2" Grid.Row="1" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnNine_Click"></Button>
Now I want to write a small helper method to run when each button is clicked but I cannot work out how to use the Content from the XAML.
for example this works for button 1 :
private void btnOne_Click(object sender, RoutedEventArgs e)
{
addButtonNumberToResult();
}
And the helper method uses btnOne.Content but how do I get it to use the Content from any button being pressed? So I can just add the helper methosto each button?
private void addButtonNumberToResult() //helper method
{
if (btnClicked == true)
{
btnTotal.Text = "";
btnClicked = false;
}
QuantityResult = QuantityResult += btnOne.Content;
btnTotal.Text = QuantityResult;
}
You can use the sender variable to determine the button clicked:
private void btn_Click(object sender, RoutedEventArgs e)
{
addButtonNumberToResult((Button)sender);
}
add btn_Click to the Click event of each button (and replace the ones you have already)
private void addButtonNumberToResult(Button btn) //helper method
{
if (btnClicked == true)
{
btnTotal.Text = "";
btnClicked = false;
}
QuantityResult = QuantityResult += btn.Content;
btnTotal.Text = QuantityResult;
}

save and load attribute to xml

Here is my code:
settings.xaml
<TextBox x:Name="txtNumber" PreviewMouseDown="myKeyPad_PreviewMouseDown" Height="34" HorizontalAlignment="Left" Margin="-253,-123,0,0" VerticalAlignment="Top" Width="134" Background="{DynamicResource AccentColorBrush}" Controls:TextboxHelper.Watermark="Enter text here too...." />
<TextBox PreviewMouseDown="myKeyPad_PreviewMouseDown" Height="34" HorizontalAlignment="Left" Margin="-114,-123,0,0" VerticalAlignment="Top" Width="134" Background="{DynamicResource AccentColorBrush}" Controls:TextboxHelper.Watermark="Enter text here too...."/>
<TextBox x:Name="rtb1" PreviewMouseDown="myKeyPad_PreviewMouseDown" Height="34" HorizontalAlignment="Left" Margin="-253,-84,0,0" VerticalAlignment="Top" Width="134" Background="{DynamicResource AccentColorBrush}" Controls:TextboxHelper.Watermark="Enter text here too...." />
<TextBox PreviewMouseDown="myKeyBoard_PreviewMouseDown" Height="34" HorizontalAlignment="Left" Margin="-114,-83,0,0" VerticalAlignment="Top" Width="134" Background="{DynamicResource AccentColorBrush}" Controls:TextboxHelper.Watermark="Enter text here too...."/>
<StackPanel Margin="0,0,0.4,0.2">
<StackPanel Orientation="Horizontal">
<Label Content="ID:" Margin="5" Width="50"/>
<TextBox x:Name="txtId" Margin="5" Width="200"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Name:" Margin="5" Width="50"/>
<TextBox x:Name="txtName" Margin="5" Width="200"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Age:" Margin="5" Width="50"/>
<TextBox x:Name="txtAge" Margin="5" Width="200" Controls:TextboxHelper.Watermark="Enter text here too...."/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Wolle:" Margin="5" Width="50"/>
<TextBox x:Name="txtWolle" Margin="5" Width="200" Custom:TextboxHelper.Watermark="Enter Wolle here too...."/>
</StackPanel>
<Button Content="Save" x:Name="Save" Click="Save_Click" Width="100" Height="30" Margin="97,5,97.4,5"/>
</StackPanel>
</Grid>
the Setting.xaml.cs show so:
private void Save_Click(object sender, RoutedEventArgs e)
{
string id = this.txtId.Text;
string name = this.txtName.Text;
string age = this.txtAge.Text;
string Wolle = this.txtWolle.Text;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
//Create a root element
XmlNode rootNode = xmlDoc.CreateElement("User");
//Create a sub element
XmlNode subNode = xmlDoc.CreateElement("User");
//Create subNode's attribute
XmlAttribute idAttribute = xmlDoc.CreateAttribute("Id");
idAttribute.Value = id;
XmlAttribute nameAttribute = xmlDoc.CreateAttribute("Name");
nameAttribute.Value = name;
XmlAttribute ageAttribute = xmlDoc.CreateAttribute("Age");
ageAttribute.Value = age;
XmlAttribute WolleAttribute = xmlDoc.CreateAttribute("Wolle");
WolleAttribute.Value = Wolle;
subNode.Attributes.Append(idAttribute);
subNode.Attributes.Append(nameAttribute);
subNode.Attributes.Append(ageAttribute);
subNode.Attributes.Append(WolleAttribute);
rootNode.AppendChild(subNode);
xmlDoc.AppendChild(rootNode);
xmlDoc.Save("User.xml");
MessageBox.Show("Created Successful");
}
how i can read the Attributes back in my TextBox on Application Start?

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);
}));

Categories

Resources