PasswordBox loses its content on navigation - wpf

I'm using a PasswordBox on a Page. Because of the implemented workflow the user can navigate to sub pages (NavigationWindow) and than return with GoBack() to the main page.
But when doing that, the password box is always empty! My job is to prevent that behaviour, but at the moment I have no clue how do achive that.
It would be great if you could help me out.
Thanks

It is a feature.
See: How to bind to a PasswordBox in MVVM
To enable the backward navigation the state of the page needs to be stored. And that is not secure.

I don't think his exact problem is a feature, but a bug of the navigation service.
In your code behind you have no easy way to distinguish between the navigation control blanking your password on navigation or the user blanking it by deleting it from the box.
So if you don't consider that, your password in your viewmodel will always be blank if you navigate to another page.
I used this hack to determine who called my password changed handler to update the view model:
private void PasswordBox_OnPasswordChanged(object sender, RoutedEventArgs e)
{
StackTrace stack = new StackTrace();
StackFrame[] stackframes = stack.GetFrames();
foreach (StackFrame stackFrame in stackframes)
if(stackFrame.GetMethod().Name == "Navigate")
return;
ViewModelPassword = PasswordBox.SecurePassword;
....
Take a look here too: http://www.wpfsharp.com/2011/04/08/wpf-navigationservice-blanks-passwordbox-password-which-breaks-the-mvvm-passwordhelper/

Related

WPF Tab_selectionChanged

I have this strange issue I am facing currently.
I have created an WPF application based on WPF page navigation. I have few button and depending on the button click the the user is navigated to respective WPF page.
In these WPF pages I have Tab controls and have used selectionchanged event handler to perform some task.
Now to the issue,
When I try to go a particular page, the selectionchanged event is also executed even before the page is loaded completely, I have tried to use the windows.loaded (based on the answer provided to my previous question - here) - I have no luck.
[I am using WPF Navigation framework]
Somehow the selectionchanged event is executing twice.
How do I stop this from happening?
I think you should check SelectionChange.AddedItems and SelectionChange.RemovedItems to find the difference between these to firings. I guess that when you select a page, SelectionChange.RemovedItems==0 while when you click on a tabItem to select it, SelectionChange.RemovedItems==1. if so just write:
if (SelectionChange.RemovedItems==0)
return;
Edit1: Please see the first comment.
Edit 2
void tablcontrol_SelectionChange(object sender, SelectionChangedEventArgs e)
{
if (e.RemovedItems.Count == 0)
{
// I guess this is the event that happens when a page is selected
// in this case just a TabItem is added to the selection
// and nothing is removed, so do nothing
return;
}
// if you are here, it means that this is another selection changed event
// so Perform those tasks that you mentioned in your question
}

In Microsoft's Toolkit DataForm, Is there any feature Prompting the User to Save When Leaving a Page

In Microsoft's Toolkit DataForm
User is trying to add a new item in Toolkit Dataform by clicking Add icon. In the middle if he selects any other menu tab, then he is going to lose all the entered info.
I want to show promt to User to Save When Leaving a Page. Like Warning user before leaving page with unsaved changes.
After some research I found the solution.
Theere is a Method called OnNavigatingFrom in Silverlight page. that methods is Called just before a page is no longer the active page in a frame.
So you can add any alert or confirm message in that methods.
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
if (DataForm.IsEditMode)
{
System.Windows.Browser.HtmlPage.Window.Alert("Please Save or Cancel changes before switching the page");
e.Cancel = true;
}
base.OnNavigatingFrom(e);
}
that will be called when you want to move to other page from current page edit or add mode.

Reset page to initial form

I load a page in my app, the user does some modifications on it. By the push of a button on that certain page, I want to reset the page to its initial form. I've tried NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.RelativeOrAbsolute)); but it doesn't work, since the From and To pages are the same. Also, I have thought of putting a new page in between, but it sounds bad. Also, it would be best if I didn't have to screw the Navigation History (i.e. the back button) by navigating without reason to another page. So any solution for a page reset? Thanks.
I found the right answer. In order to reset the page to its initial form you have to do this:
void buttonResetPage_Click(object sender, RoutedEventArgs e)
{
this._contentLoaded = false;
InitializeComponent();
// other initializations found in your page constructor
}
It worked for me. :)
I did it in the following way:
ContentFrame.Content = New {pagename}
this sets the contentFrame, which contains my webpages page1,page2 etc, to a brand new instance of that page. This then reloads everything on that page, the layout and also gets the data back from the database again. This way worked for me.

Prevent Multiple Form Instances

How do I prevent Multiple forms from opening?
I do .show on the form but the user can click the main form and the button again and another instance of form opens.
Two options, depending on what you need:
Use ShowDialog instead of Show, which will open a modal window. This is the obvious solution if you don't need your main form to be active while the child form is open.
Or keep track of the window you opened already in the main form and do nothing if it's already open. This will be needed if you want the user to be able to use the main form while the child form is already open, maybe to open other forms.
do something like:
SingleForm myform = null;
void ShowMyForm_Click(object sender, EventArgs e)
{ if (myform == null)
{
myform = new SingleForm();
}
myform.Show();
myform.BringToFront();
}
Force your form object to adhere to the singleton pattern
I prefer to use Generics and lazy loading to handle my forms. Since all of my forms inherit from a base class, I can use the same method to bring forms to the front, send them to the back, destroy them, start them, etc.
If you keep a form manager class that's responsible for managing any loaded forms, you can bring whatever form to the front that you want, or prevent specific forms from being able to come back unless certain criteria are met.
public void LoadForm<T>() where T : MyNameSpace.MyBaseForm
{
// Load all your code in this joint and just call it when you
// need a form. In here, you can determine if a copy of the form
// already exists and then bring it forward or not
}
Disable the main form until the child form goes away, or disable the button.
button_onClick(object Sender, EventArgs e)
{
Button btn = sender as Button;
btn.Enabled = false;
Form myform = new MyForm();
myform.Show();
}
Of course, you really should be using form.ShowDialog() rather than form.Show() if you want modal behavior.

UI Design Concepts in WinForms

In one certain case I want to disable the tabpannel so that the controlls in the tab panel are disabled.
I want to disable the tab pannel but still I want ennable the controls in tab pannel.the need is User cannot switch over to the annother tabpannel in a certain senerio.
How can I do this requirement?.
by
dinesh
Use something besides a tab panel.
It is not standard behavior for a tab panel to have one tab "stuck" so that the user cannot move to the other tabs. You're going to throw users off if you do this.
What you are after sounds like a modal dialog. It sounds like you don't want the users to move away from a certain screen until they're doing entering some data or some such. The modal dialog is built for this purpose.
There is no direct way to disable Tab Page, only you can remove it. But in your case, you can't remove the Tab, So I think you need to put some code in the Tab_SelectionIndex change event. And when ever the Tab index comes, set it back to another one.
Try this code
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedIndex == 1)
{
tabControl1.SelectedIndex = -1;
}
}
In addition to anuraj's answer, set the colour of the tab text to the disabled text colour state, so it is a visual cue it is "disabled".

Resources