WPF Passwordbox with Update Source Trigger - c#

Currently I'm doing a registration to create a new user to login, but to create an user I would like to have updatesourcetrigger on my fields just like I do for the name field for an example:
<TextBox Text="{Binding FirstName, UpdateSourceTrigger=PropertyChanged}"></TextBox>
The functions to create the user works but the updating information at the current time doesn't work really unless you press on another textbox for an example, if you do that then the information on the passwordbox will update to the new value. I am using MVVM so I don't want any code behind in the xaml.cs file. I want it to be updating its value like on the xaml code I mentioned.

Solved it by myself without any bindings or a updatesourcetrigger.
I did it with a commandparameter. With a button using a commandparameter, which means that it gets current information on the passwordboxes.

Related

AutoCompleteTextBox in WPF C#

I've seen a number of suggestions for AutoComplete in WPF on a TextBox. However, they are all years old by now and many are expired without working websites.
Basically, I'm going to want to bind the TextBox to an ObservableCollection with a Model of a Person. Their Name will be displayed wen the user types as a suggestion, and will be displayed when the user selects said Person.
However I also want to be able to access the selected Persons ID as well. Is there a TextBox that is still current and can achieve what I would like it to do?
EDIT: I've attempted to use WPF TextBox AutoComplete like so:
<TextBox Text="{Binding SelectedItem.ContactMadeBy, ElementName=contactsDataGrid, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" behaviors:AutoCompleteBehavior.AutoCompleteItemsSource="{Binding EmployeeCollection}"/>
but nothing happens when I type. I've looped through EmployeeCollection and it is definitely populated, so the issue lies with the TextBox. An additional problem to this is that it needs to be bound to an IEnumerable<string> where I can't have the Persons ID as well.

WPF C# Textbox text change update in ViewModel

Hello I am working on a simple MVVM project; a simple text/config editor that loads a config file and then it checks in the ViewModel in case the file has been changed, it enables the Save menu item simply by binding a boolean property. But here comes a problem, where I can't find any property in the textbox control that could be bound to a vm property in case a change happens in the text. I have managed to somehow simulate this by creating an event in the code-behind :
(DataContext as AnalizeSectionViewModel).ContentChanged = true;
The event is fired on any text change. But I would like to bind a property from the textbox, something like:
IsModified="{Binding ContentChanged}"
Can such a thing be done?
You should be able to just bind the Text textbox property to your model via binding
Text="{Binding MyViewModelProperty}"
Anytime the text in your textbox changes your property in your model will change which will allow you to do 'stuff' when that occurs. This will fire the property changed event when the user moves to out of the field.
Now, if the intent is for it to fire each time the user types then you can explicitly tack on the
UpdateSourceTrigger="PropertyChanged"
By setting it to PropertyChanged, you will get a notification each and every time the text changes.

Userinput in Textbox overwrites Databinding

I have a working OneWay DataBinding from a double CLRProperty to a Texttbox.Text. I don't use a TwoWay because not every userinput is acceptable. BTW I use an existing Command that impements the ICommand interface.
The problem as soon as the user types in a text in the Textbox, the Databinding is destroyed.
<TextBox Text="{Binding Path=myDouble, Converter={converter:DoubleToTextConverter}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
How should your programm know if the user input is acceptable or not, if you dont tell him?
To check the user input, you could do the following:
Use DataValidation to accept only certain inputs.
Create a second TextBox where the user does the input and if it's ok (you check that in your ViewModel), you apply it to the first TextBox's binded Property myDouble.

How to refresh the UI in a metro app?

I'm using this code in a XAML page:
<TextBox ItemsSource="{Binding Posters, Converter={StaticResource collectionToFirstElementConverter}, Mode=TwoWay}" />
Posters is an ObsevableCollection and I'm using a converter where takes the collection and gets the first element of it.
As I'm using async procedures, where the textbox receives the object, this one has no elements (Count=0), and calls the converter.
I'm trying to update the textbox everytime the property add new elements, but not calls the converter.
I remember that in Silverlight or WPF, exists SourceTrigger or UpdatePropertyChanged, but in WinRT I can't see this mode.
The easiest way to achieve that would be to modify your view model containing the Posters property accordingly. I can see two ways to go about it (both asuming that your view model implements INotifyPropertyChanged):
Add an event handler to Posters.CollectionChanged and inside it raise INotifyPropertyChanged.PropertyChanged for Posters.
Add another property FirstPoster returning the value of the first element in Posters. In the view model add an event handler to Posters.CollectionChanged and inside it raise INotifyPropertyChanged.PropertyChanged for FirstPoster. This way you don't even need the converter.
I personally like the second approach better.

How to forbid automatic databinding on validation error

I'm creating a simple database application in C# WPF using MVVM as Relay Commands and databinding. For in-memory storage of database content I use ObservableCollection, which is binded to the Datagrid as follows:
<DataGrid ItemsSource="{Binding Path=Softwares, Mode=OneWay}" SelectedItem="{Binding Path=SoftwareSelection, Mode=TwoWay}">
when the item is selected user can chose to edit it. For editation a form is opened with a bunch of textboxes with the data of given entity. All the fields are validated using IDataErrorInfo, unless all textboxes are valid, the ok button is not enabled, and therefore no changes can be saved to the collection and to the database.
Here is how the example textbox looks like:
<TextBox Text="{Binding Name, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True}"/>
But the tricky part is, in case I change some values in textboxes and then close the window, the new values are propagated to the ObservableCollection, which I don't want to. Do you have any idea, how to prevent such behaviour? I would like the databinding work only after clicking the button. Otherwise the databindng works well, so as the button (dis/en)abling and reflecting changes to the database and to the collection after clicking. Both views are serviced by different ViewModels, data between views are passed by firing events.
I tried to add to the DataGrid UpdateSourceTrigger=Explicit to the ItemsSource binding, but didn't help. Perhaps, I'm missing some application logic?
Thank you very much for your help.
This is where most WPF developers make mistakes of assumptions!
In MVVM dirty data can be stored in the ViewModel and that's what the layer of VM is for! It mimics the View from Model's perspective and because View is in error, the ViewModel would also be in the error. Thats perfectly valid.
So having said that, the question remains
How will you NOT allow the temporary / dirty data to flow to your
ObservableCollection?
Two ways...
If your ObservableCollection is specific to your model class (say MyItem) then if your Model class (MyItem) is an Entity class \ DAL class \ NHibernate class create a wrapper of MyItem class called ViewModelMyItem and then instead of ObservableCollection<MyItem> use ObservableCollection<ViewModelMyItem>.
This way dirty data from your View would be inside ViewModelMyItem and it can only be legitimately flown back to your model class (MyItem) ONLY when Save button is clicked. So that means in Save Command's Execute() delegate you can copy \ clone the ViewModelMyItem's properties into Item's properties, if validations in ViewModelMyItem are fine.
So if Item is an EntityType class / NHibernate class / WCF client model class, it would always only valid data as ViewModelMyItem is filtering the temporary / dirty information upfront.
You could use Explicit binding model. It stops the TwoWay data to flow back to the sorce Item unless BindingExpressions.UpdateSource() is explicitly called.
But according to me, this defeats MVVM in straightforward way because ViewModel will not have what UI is showing! Still however you can use *Attached Behavior * to govern explicit binding by staying in MVVM space!
Let me know if this helps!
You're better off putting the code into the domain object's property setter. Then synchronize with the visuals by triggering the NotifyPropertyChanged handler.
More on this topic:
http://msdn.microsoft.com/en-us/library/ms743695.aspx
http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx
Setting the Binding Mode to Explicit should require you to call the binding expressions UpdateSource() method to send changes back to your model. Because you only mentioned that you set Explicit on the DataGrid's binding, I'm guessing you only need to make sure that you have that mode explicitly set on any property that is being bound directly back to your model. Such as your TextBox's Text Binding, in the case above. That will likely fix your problem but require you to call UpdateSource() on each target's BindingExpression one way or another.
If you're using one of the mainstream ORM's (EF, Linq to SQL, etc), then chances are your Entities automatically implement INotifyPropertyChanged and INotifyPropertyChanging. Because you are sharing a reference to your single instance, all changes in your edit will be reflected in your main view and anything else Binding to that instance. As a dirtier alternative, you can just create a separate instance of the same type and manually copy the values back over when the window's dialog result is true.
The first approach requires you to manually update the bindings, the second approach requires you to manually update the values from the Edit's instance.
With some more code, I can help with your specific approach.

Categories

Resources