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.
Related
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.
I've a table in a database where I need to show all the rows's content (as a TextBlock) and to the right of each TextBlock I need to show a TextBox so the user can enter a value (a number) for each row and also I need to be able to change the color of any TextBox when the value provided by the user is negative.
Can someone give me a clue with this?
PD: I'm using WPF with Prism 4 and MVVM pattern and VS2010 Ultimate
I won't give you a complete solution, but I can point you in the right direction.
I start by creating a data structure that contains properties for Name and Value, and that implements INotifyPropertyChanged for Property Change notification.
Next in the ViewModel (or possibly Model), I would make would be an ObservableCollection<MyDataObject>, and populate it with the data from the database.
In the XAML, I would use an ItemsControl bound to the collection, and overwrite the ItemTemplate to render each item as either a Horizontal StackPanel or a Grid, containing the Label and TextBox
For the TextBox.Foreground property, I would bind it to the same value that TextBox.Text is bound to, except I'd also use a IValueConverter in the binding which checks to see if the value is above or below 0, and returns the correct color. Since it is a binding, it will automatically update whenever the value changes.
<TextBox Text="{Binding Value}"
Foreground="{Binding Value, Converter={StaticResource MyCustomConverter}}" />
It'd be nice to know what you've tried so we can help you better, but you almost certainly want to be binding your data to a DataGrid or, if you absolutely need more flexibility (so far it doesn't sound like that's the case), an ItemsControl. You don't want to be just creating a Grid dynamically.
Your question about the TextBox and how to change it's color when the value is negative is actually a separate question from how to do your layout. I'd look into the Validation components of WPF for that.
I have a ComboBox whose ItemSource is bound to a list of strings (idealy i would use an Enum), this is done using the MVVM pattern.
Now i want to bind an object to the ComboBox, it's called SelectedUser and i want to bind its property: UserType, which is a string.
So i have got this:
<ComboBox ItemsSource="{Binding Path=Usertypes}" SelectedValue="{Binding Mode=TwoWay, Path=SelectedUser.UserType}" />
It works and it does change the value of the selected user if i play with it, but the problem is, that it does not display anything in the ComboBox unless i select a user, and then change the ComboBox selection, then it works, but only for that user.
I tried playing around with DisplayMemberPath, SelectedValuePath and SelectedItem,
when i added those the ComboBox did not show anything in it (there were still options to select from, but they were invisible or something).
So what should i do? Is this a bug?
I have to mention that i have got another ComboBox that has a list of ints, and it works fine.
Update:
I was informed that I'm getting this issue because the string I'm comparing to the string in the comboBox, are not actually the same.
My string comes from the Entity Framework via Ria Services. (User.UserType)
And when it compares it to the list of strings in the ComboBox ItemSource, they are not equal, for some strange reason.
And i also heard, i might have to override Equal method for that check.
but I'm not sure where and how to do so.
Is the view notified if the SelectedUser changes? I could imagine that this might be the problem; if there is no such notification the ComboBox will not reload the SelectedValue if another user is selected, it will only update the binding if you make changes yourself.
If that is not it, you also need to consider that no selected value will be displayed unless the current value exactly matches one of values in the source list.
I need to be able to get and set the textbox's text, but I don't want every single change in the textbox's text to update a string in my viewmodel. It seems to me that binding a string to the textbox's text property would be nearly as inefficient as updating a string on a textbox's textchanged event or am I wrong?
The default UpdateSourceTrigger for bindings to TextBox.Text is LostFocus, which means that the string in your viewmodel is only updated when the TextBox is left.
Of course, if you do something like this
<TextBox Text="{Binding Path=ItemName, UpdateSourceTrigger=PropertyChanged}" />
then your string will be updated every time something changes in the text box. So, yes, it would be "nearly as inefficient as updating a string on a textbox's textchanged event".
Still, I don't see a real-world problem here: Even if you have a very fast typing user, updating a string in memory is not an operation that takes a terrible amount of time. In fact, processing the user input by the TextBox itself (showing the characters typed, scrolling the text to the left if the end of the box is reached, etc.) also needs quite a bit of processing, so I don't think updating your string creates such an unreasonable additional burden.
I would think it really depends what you define as efficient. To me, being able to declare a two-way binding declaratively on the XAML level is a lot more efficient than handling the text changed event and attaching myself to the property changed of a view model and copying stuff back and forth.
Why would I do that?
Apart from that, the default binding behaviour is on focus lost...
I have code like this, in pagefunction making up a wizard:
<TextBox Name="txtDate" Text="{Binding Path=Date}"></TextBox>
The user types in and clicks next, object sent to next page with this data.
But if I set
<TextBox Name="txtDate" Text="{Binding Path=Date, TargetNullValue='2010-01-15'}"></TextBox>
So I can have some "example" text in the box already, and the user hits next to accept this without changing it, no value is passed in the object to the next pagefunction. If the user changes it then it works as usual.
So how can I have some default text without stopping the data being sent on?
You could set an initial value on the date that you are binding to.
If the Date property is a dependency property when you create it you can give it an inital value. Then you could attach a handler to notify you when it changed, and mark a flag as non intial value. (so you know its changed)
If you're providing a true data default (i.e. one that is a valid data value) consider initializing your data-bound object to that value.