Having trouble entering time values with MaskedTextbox - wpf

I'm using the MaskedTextbox for .NET 3.5 SP1, from the WPF toolkit. I've got a MaskedTextbox on a WPF page, it fills fine from a SQL Server database, but when I tried to edit the value nothing at all happens. I can select anything, but typing over it, or pressing the delete key or anything else I can thing of, does nothing to the value there. I want to use this to allow a user to enter or edit a time value, like this: 9:30 AM would appear as "09:30 AM". Here's the XAML that I've specified:
<cusControls:MaskedTextBox x:Name="mtbTime"
Mask="90:00 >LL" Margin="5,0,0,0"
Text="{Binding ElementName=ThisDateTime,Path=TimePart,Converter={StaticResource NullableTimeToUnderscoreConverter}}" />
(This is a part of a user control I'm making called "ThisDateTime".) What am I doing wrong and how do I fix it?

I haven't seen the source for the text box, but ran into something similar myself.
Could it be a problem with your partially-entered text either not matching the mask, or not matching the converter?
Is the UpdateSourceTrigger on the Binding somehow set to UpdateProperty, or is something similar happening in the code-behind?

Related

How to use a control equivalent to the WinForms DataGridView in WPF

I've been creating an inspection form using WPF and I need a place where users can type an unknown amount of comments (hence why I'm not using textboxes). In my WinForms version of this application, I used a DataGridView and I could enter in as much information as I wanted to. I'm looking to do the same with a DataGrid or an equivalent control in WPF.
WinForms Example
I need to be able to do the same thing in WPF but I can't seem to add any rows in the DataGrid. On top of that, when I try to check CanUserAddRows it unchecks it immediatly.
So I checked out Vincent Sigal's blog post about this issue. He mentions something interesting:
... but beware of CanUserAddRows and CanUserDeleteRows as they can appear a little magical. Their values are coerced based on other properties such as DataGrid.IsReadOnly, DataGrid.IsEnabled, IEditableCollectionView.CanAddNew, and IEditableCollectionView.CanRemove. So this is another thing to watch out for when editing. If you run into a situation where you set CanUserAddRows or CanUserDeleteRows to true but it is changed to false automatically, check that the conditions below are met.
I verified this and my DataGrid is not read-only and it is enabled. Although, I have no idea where to find the IEditableCollectionView.CanAddNew and IEditableCollectionView.CanRemove ...
I don't think my situation should require a binding event on the DataGrid since the user is supposed to enter his comments directly into the DataGrid ... Is what I'm trying to do even possible? Perhaps I should use a different control?
I have to admit that I stopped reading through your question after the first paragraph, so please forgive me if I have understood you wrong... but if you just want to enter multi line text into a TextBox in WPF, you can do it by setting a couple of properties on it:
<TextBox TextWrapping="Wrap" AcceptsReturn="True" />
For a DataGrid, you can set these properties in the DataGridTextColumn.ElementStyle and/or DataGridTextColumn.EditingElementStyle as the WPF DataGridTextColumn multi-line input post shows quite nicely.
Please let me know if I did misunderstand you.
UPDATE >>>
Ok, so I came back to read the rest of your question... answering without reading the question can be risky business on this site. It's just as well that I did too, as I see you also want to know how to use the DataGrid.
I have to start by saying... take a deep breath... WPF is very different to WinForms... very different. In WPF we manipulate data rather than UI objects, so to add a new row actually means adding a new item to a collection. You can find a complete working example on the DataGrid Class page on MSDN.
Please also view the WPF DataGrid Control page on WPF Tutorial.NET for more examples. WPF has a lot to take in for new comers and can be quite bewildering, but it's well worth the trouble when you get into it.

ComboBox autocomplete strange behaviour

Bear with me. I have a ComboBox (WPF) in 'autocomplete' mode.
Easy peasy right? Just set IsEditable to true.
<ComboBox
IsEditable="True"
Text="{Binding Model.TextEntered}"
ItemsSource="{Binding Model.ListWithOptions}"/>
I've got my ComboBox and it autocomplete's just perfect.
BUT when I enter the following text 'caaaaa' it changes it to 'CAaaaa'. I understand the ComboBox autocomplete's the text and match the casing. But when I want to enter caaaaa (for whatever reason) I don't want 'CAaaaa'.
Any idea to solve this?
As an alternative I would settle for (if all else fails):
When leaving the control, check if entered text doesn't match any of the options set text tolower. (as a behaviour)
Have you tried setting ShouldPreserveUserEnteredPrefix="True"? This will not alter the case that is typed into the Combobox but will still match items in the list. So in your example if you type 'ca13' it will find and select 'CA132S', but it will be displayed in the combobox as 'ca132s' (in the case that it was typed in).
A compromise in the way that the item is displayed perhaps, but it will select the correct item and allow you to type characters in either case without alteration.
Try setting IsTextSearchCaseSensitive proprty to true for the ComboBox. Not sure if this property is available in older versions of .NET
Couldn't you use an if statement to check if it matches anything in the list and if it doesn't, don't change it? Like check after each letter entered and if it then doesn't match change it back to the case that was originally entered?
Or is there a reason you want people to be able to type in something that isn't in the list? If not, couldn't you use IsTextSearchEnabled rather than IsEditable?

Bind textbox text to disk path of xml file

I have a window set up that shows the data in an xml file and allows the user to change it, save, etc. I have pretty much evertything set up except that I would like to have a textbox or textblock that shows the file path of the current file that the user is working with.
I have an XmlDataProvider set up called 'xmlData' and I have the text binding as follows:
Text="{Binding Path=Source, Source={StaticResource xmlData}}"
I have also tried the datacontext to the static resource above and both just give me a blank textbox even though I know there is an xml file in the data provider (I can write to it). Am I missing something or can this not be done this way?
The problem is that XmlDataProvider.Source is a Uri not a string. So you will want to do something like:
Text="{Binding Path=Source.AbsolutePath, Source={StaticResource xmlData}}"
Absolute path isn't going to give you something like: C:\My documents\test.xml however, it's more likely to be something like: file://c:/my documents/test.xml. So you'll need to find a good way to manipulate the source Uri to get what you want.
One thing you could do is use a Converter to parse it and return what you want.
It looks like Uri.LocalPath might also give you what you want, but I'm not 100% sure without testing it.
Ok well I figured something out that works but it's not what I wanted. I ended up going 'old school' with it. I put a standard property on my form in the code behind with a backing variable and get/set methods. In the set method I just set the text property to the same value as the backing variable.
I'm waiting to mark this as the answer to see if someone has a better solution.
--What I tried--
After the above answer/conversation I tried sticking a converter in that simply returned the value back so that I could break into it in the debugger and see what was being passed to the converter. No matter what I did I couldn't get it to hit the breakpoint so it seems like it wasn't even getting to the converter for some reason.
I then tried to define a dependency property on the code behind of the form itself and a standard property as a wrapper (where the get and set just use the GetValue and SetValue on the dependency property) and bind the textbox text to that using relativesource and findancestor. Basically I bound the text to that property of the form. Then I changed the property using the wrapper in code behind whenever the user browsed for a new file. This worked on the load up, but whenever I set the value in code it didn't update the text in the textbox. I even set the binding tracing to high using diagnostics; I saw that it found the form and said it bound to the value, etc. but whenever I hit the browse method and change the path I get no output and no change in the text.
Like I said, what I have is working and it's reasonable I guess, but if anyone can add insight as to why this wouldn't work I would be very interested to know. I am fairly new to WPF so I want to learn to do these things right...

WPF datagrid not refreshing like asked

I have a weird situation in one of my program when a datagrid's content is not update when I call the grid.items.refresh command.
I do know that the source of the grid is updated as it should and should I quit the windows and reopen it, which has the effect of giving the itemssource back to the datagrid, the grid is now OK.
Are there any known bug with the items.refresh command of the datagrid or is there something I am not doing right ?
Thanks,
Edit : Forgot to mention, this only seems to happen on my client computer. As usual, it's working here, but I did see the problem myself. I just can't explain it.
Some suggestions:
On your DataGrid column definitions, you can add UpdateSourceTrigger=PropertyChanged to your Column Bindings.
ie: Binding="{Binding Path=Product.ProductCode, UpdateSourceTrigger=PropertyChanged}"
If that does not help, consider changing your List to an ObservableCollection<OfSomething>. Then WPF's data binding will automatically react to changes within your source.

TextBox inputing data issue with MVVM Light.?

I am working on WPF MvvmLight application and once I click on first page it navigates to second page. The second page has a textbox on which the focus is set. On this textbox, I am not able to put any data or any character. But I can do copy and paste. What might be the reason for this strange behavior.
<TextBox VerticalAlignment="Center" x:Name="txtsearchYouTube" Height="25"
Margin="100,0,100,0" Canvas.Top="275" Width="500"
Loaded="txtsearchYouTube_Loaded" Canvas.Left="50"
Text="{Binding SeachKeyWord,Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"/>
1) The Binding is not the issue, in fact even if it is wrong, you will still be able to write in that TextBox, it just won't be put in SearchKeyWord. By the way, you mispelled it (you forgot an R in seaRch).
2) If you're really only setting the focus on the loaded event, and nothing else, then it's not the problem either.
3) MVVM Light is just a conveniency framework, it does not alter WPF for you as it just provides tools, it definitely doesn't mess with TextBoxes.
That said, here is what I would check in your situation:
If SearchKeyWord is a dependency property look where it is declared to see if there is a callback/coercevalue/validatevalue declared. Since the binding is two-way, it could very well always invalidate the values recieved (preventing you from actually writing anything in there)
Check all the way up your visual tree, for any implicit styles for the TextBox type. They could have triggers that mess up with your data.
Hope this helps,
Bab.
i dont know whats wrong with your code. but you can help your self. so just remove your binding and loaded event and check if you can put any data in your textbox.
if yes pls post all code from your event and viewmodel otherwise its hard to help.
you wrote you can copy and paste? what does it mean? your textbox show the text you paste in? or your viewmodel got the pasted value?
EDIT: you can also use Snoop to check your textbox binding and properties at runtime.

Resources