Best Way To Convert A Value Based On The Type In A Different Control
-
Good People, I have a control that records the amount of time of a specific event - which the user enters. However, the user can also choose the type of time span. For example, in the duration they can enter the number '5'. Then, they can select the type of time span as "Days", "Hours", or "Minutes". In order to facilitate comparisons, I have to convert all of the entries to minutes when it is written to the database; then convert them back to the chosen time span type when I must display it to the user. My question is: what's the best way to do this? I thought about using a value converter, however I don't know how to make a value converter grab the type selected in a different control. Plus, I have problems with value converters because I don't know how to make it wait until the user finishes typing if they are entering more than one character. Any help you can provide would be greatly appreciate. Thanks. Blitz
-
Good People, I have a control that records the amount of time of a specific event - which the user enters. However, the user can also choose the type of time span. For example, in the duration they can enter the number '5'. Then, they can select the type of time span as "Days", "Hours", or "Minutes". In order to facilitate comparisons, I have to convert all of the entries to minutes when it is written to the database; then convert them back to the chosen time span type when I must display it to the user. My question is: what's the best way to do this? I thought about using a value converter, however I don't know how to make a value converter grab the type selected in a different control. Plus, I have problems with value converters because I don't know how to make it wait until the user finishes typing if they are entering more than one character. Any help you can provide would be greatly appreciate. Thanks. Blitz
BlitzPackage wrote:
I don't know how to make it wait until the user finishes typing if they are entering more than one character.
By default, the Text property on TextBox controls doesn't update the source until the control loses focus, specifically for this reason. (if you find this is NOT the default, you can set the UpdateSourceTrigger to LostFocus on the binding)
BlitzPackage wrote:
I thought about using a value converter, however I don't know how to make a value converter grab the type selected in a different control.
Converters can take a converter parameter, but unfortunately you can't use a binding in XAML to pass that parameter value. If the time-span type selection controls are bound to a property in the same data object as the desired source property for the time span binding, then you could bind to the entire object instead of the single property. Then your converter would have access to the both the type and span properties in that data object. You could also bind to the DataContext of any element in the tree if there's a place to get the required properties from. Just using code on the textbox's LostFocus event would be relatively simple as well. The best way? I don't know :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
BlitzPackage wrote:
I don't know how to make it wait until the user finishes typing if they are entering more than one character.
By default, the Text property on TextBox controls doesn't update the source until the control loses focus, specifically for this reason. (if you find this is NOT the default, you can set the UpdateSourceTrigger to LostFocus on the binding)
BlitzPackage wrote:
I thought about using a value converter, however I don't know how to make a value converter grab the type selected in a different control.
Converters can take a converter parameter, but unfortunately you can't use a binding in XAML to pass that parameter value. If the time-span type selection controls are bound to a property in the same data object as the desired source property for the time span binding, then you could bind to the entire object instead of the single property. Then your converter would have access to the both the type and span properties in that data object. You could also bind to the DataContext of any element in the tree if there's a place to get the required properties from. Just using code on the textbox's LostFocus event would be relatively simple as well. The best way? I don't know :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
Thanks for your reply. I will look into it. Blitz
-
Good People, I have a control that records the amount of time of a specific event - which the user enters. However, the user can also choose the type of time span. For example, in the duration they can enter the number '5'. Then, they can select the type of time span as "Days", "Hours", or "Minutes". In order to facilitate comparisons, I have to convert all of the entries to minutes when it is written to the database; then convert them back to the chosen time span type when I must display it to the user. My question is: what's the best way to do this? I thought about using a value converter, however I don't know how to make a value converter grab the type selected in a different control. Plus, I have problems with value converters because I don't know how to make it wait until the user finishes typing if they are entering more than one character. Any help you can provide would be greatly appreciate. Thanks. Blitz
In that kind of situation, I would put a DependencyProperty on the value converter and bind that to the radio buttons. In this case, the property would probably be an enum TimeType or some such and the control used to select the units would be bound to the property on the converter. I do not know if changing the TimeType property on the converter will update the bound data property, so that may be something you will have to look into.