WPF TextBox: how to implement a Value property like in VO - Try 1
Posted: Wed Dec 27, 2017 7:19 pm
When working in WPF, I was missing the possibility to assign a numeric or date value to a textbox and have the same type of value returned - the TextBox has only a :Text property that returns a text.
Using MVVM, you can assign also numeric or date values, but the way back is not in your control.
My first try was to implement a :Value property and detect the type of the value automatically:
In the return the conversion can be made using Reflection:
You can find the complete implementation together with a sample attached to this message as XIDE export file.
But the shown solution works well if you are using WPF without MVVM - in the next days I will show a IMHO better solution based on a generic TypeConverter.
Wolfgang
Using MVVM, you can assign also numeric or date values, but the way back is not in your control.
My first try was to implement a :Value property and detect the type of the value automatically:
Code: Select all
property _value as object
get
return _oValue
end get
set
_oValue := value
self:_SetType( oValue )
end set
end property
private method _SetType( oValue as object ) as void
_oValueType := oValue:GetType()
self:Text := oValue:ToString()
return
Code: Select all
private method __Update() as void
local oValue as object
local oInfo as System.Reflection.MethodInfo
local lSuccess as logic
local aParameters as object[]
local cTextValue as string
local aTypes as System.Type[]
aTypes := <System.Type>{ TypeOf( string ), ( ( System.Type ) _oValueType ):MakeByRefType() }
oInfo := _oValueType:GetMethod( "TryParse", aTypes )
cTextValue := self:Text
oValue := null
aParameters := <object>{ cTextValue, oValue }
lSuccess := ( logic ) oInfo:Invoke( null, aParameters )
if lSuccess
oValue := aParameters[__ARRAYBASE__+1]
self:Value := oValue
_cTextValue := cTextValue
else
self:Text := _cTextValue
endif
return
But the shown solution works well if you are using WPF without MVVM - in the next days I will show a IMHO better solution based on a generic TypeConverter.
Wolfgang