String To Double Converter with IValueConverter C#
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a View in which I should be able to input doubles. The thing is, I can only input whole numbers such as "100" but not "100.4". All my background calculations run on doubles though.
I'm now trying to bypass the problem by implementing a StringToDoubleConverter but my C# knowledge is still very limited.
I've implemented this into my UserControl.Resources
tag
<local:StringToDoubleConverter x:Key="StringToDouble"/>
and created a new class StringToDoubleConverter:
class StringToDoubleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string stringNumber = value as string;
double.TryParse(stringNumber, out double val);
return val;
}
}
Finally I've implemented the converter into my binding:
<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DelayModel.DelayTime, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource StringToDouble}}"/>
My DelayTime
in my DelayModel
looks like this:
private double _delayTime;
public double DelayTime
{
get
{
return _delayTime;
}
set
{
if (value != _delayTime)
{
_delayTime = value; NotifyPropertyChanged();
}
}
}
I know my converter is somehow wrong. I'm struggling to get the right code to convert the string I want to input in my View to doubles.
For example: I want to input "0.7" into my View and DelayTime
should actually get "0.7" and not just the "7". Is TryParse oder double.Parse(value) correct?
c# string double ivalueconverter converters
|
show 4 more comments
I have a View in which I should be able to input doubles. The thing is, I can only input whole numbers such as "100" but not "100.4". All my background calculations run on doubles though.
I'm now trying to bypass the problem by implementing a StringToDoubleConverter but my C# knowledge is still very limited.
I've implemented this into my UserControl.Resources
tag
<local:StringToDoubleConverter x:Key="StringToDouble"/>
and created a new class StringToDoubleConverter:
class StringToDoubleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string stringNumber = value as string;
double.TryParse(stringNumber, out double val);
return val;
}
}
Finally I've implemented the converter into my binding:
<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DelayModel.DelayTime, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource StringToDouble}}"/>
My DelayTime
in my DelayModel
looks like this:
private double _delayTime;
public double DelayTime
{
get
{
return _delayTime;
}
set
{
if (value != _delayTime)
{
_delayTime = value; NotifyPropertyChanged();
}
}
}
I know my converter is somehow wrong. I'm struggling to get the right code to convert the string I want to input in my View to doubles.
For example: I want to input "0.7" into my View and DelayTime
should actually get "0.7" and not just the "7". Is TryParse oder double.Parse(value) correct?
c# string double ivalueconverter converters
4
What culture is your PC set to? Does it use a period for decimal point?
– jdweng
Jan 3 at 12:13
I'm german. I want to use "." or "," for decimal points. Either one is fine. But right now I can't enter either of them. What I can do is: type: "1" then use the left arrow key and write a ".", the program will automatically put a 0 in front. It now shows "0.1" and it works. But thats not very intuitive...
– jan97con
Jan 3 at 12:17
1
your problem is using UpdateSourceTrigger=PropertyChanged. It update the source after pressing each key. If you put the point character, it update the source with string value "0." what give you back "0" without point. You can change UpdateSourceTrigger to Default and update Source manually when you press Enter.
– Stefan W.
Jan 3 at 12:26
One should work and the other fail. Not both fail. If you want bot the work then us TryParse and when one fails then use different culture and try second one. Or change period/comma to other one using string method.
– jdweng
Jan 3 at 12:31
1
You can try thisdecimal.TryParse(stringNumber, NumberStyles.Any, CultureInfo.InvariantCulture out decimal val)
.CultureInfo.InvariantCulture
will take care of the decimal character, whatever it is.
– ikerbera
Jan 3 at 12:53
|
show 4 more comments
I have a View in which I should be able to input doubles. The thing is, I can only input whole numbers such as "100" but not "100.4". All my background calculations run on doubles though.
I'm now trying to bypass the problem by implementing a StringToDoubleConverter but my C# knowledge is still very limited.
I've implemented this into my UserControl.Resources
tag
<local:StringToDoubleConverter x:Key="StringToDouble"/>
and created a new class StringToDoubleConverter:
class StringToDoubleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string stringNumber = value as string;
double.TryParse(stringNumber, out double val);
return val;
}
}
Finally I've implemented the converter into my binding:
<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DelayModel.DelayTime, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource StringToDouble}}"/>
My DelayTime
in my DelayModel
looks like this:
private double _delayTime;
public double DelayTime
{
get
{
return _delayTime;
}
set
{
if (value != _delayTime)
{
_delayTime = value; NotifyPropertyChanged();
}
}
}
I know my converter is somehow wrong. I'm struggling to get the right code to convert the string I want to input in my View to doubles.
For example: I want to input "0.7" into my View and DelayTime
should actually get "0.7" and not just the "7". Is TryParse oder double.Parse(value) correct?
c# string double ivalueconverter converters
I have a View in which I should be able to input doubles. The thing is, I can only input whole numbers such as "100" but not "100.4". All my background calculations run on doubles though.
I'm now trying to bypass the problem by implementing a StringToDoubleConverter but my C# knowledge is still very limited.
I've implemented this into my UserControl.Resources
tag
<local:StringToDoubleConverter x:Key="StringToDouble"/>
and created a new class StringToDoubleConverter:
class StringToDoubleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string stringNumber = value as string;
double.TryParse(stringNumber, out double val);
return val;
}
}
Finally I've implemented the converter into my binding:
<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DelayModel.DelayTime, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource StringToDouble}}"/>
My DelayTime
in my DelayModel
looks like this:
private double _delayTime;
public double DelayTime
{
get
{
return _delayTime;
}
set
{
if (value != _delayTime)
{
_delayTime = value; NotifyPropertyChanged();
}
}
}
I know my converter is somehow wrong. I'm struggling to get the right code to convert the string I want to input in my View to doubles.
For example: I want to input "0.7" into my View and DelayTime
should actually get "0.7" and not just the "7". Is TryParse oder double.Parse(value) correct?
c# string double ivalueconverter converters
c# string double ivalueconverter converters
asked Jan 3 at 12:10


jan97conjan97con
257
257
4
What culture is your PC set to? Does it use a period for decimal point?
– jdweng
Jan 3 at 12:13
I'm german. I want to use "." or "," for decimal points. Either one is fine. But right now I can't enter either of them. What I can do is: type: "1" then use the left arrow key and write a ".", the program will automatically put a 0 in front. It now shows "0.1" and it works. But thats not very intuitive...
– jan97con
Jan 3 at 12:17
1
your problem is using UpdateSourceTrigger=PropertyChanged. It update the source after pressing each key. If you put the point character, it update the source with string value "0." what give you back "0" without point. You can change UpdateSourceTrigger to Default and update Source manually when you press Enter.
– Stefan W.
Jan 3 at 12:26
One should work and the other fail. Not both fail. If you want bot the work then us TryParse and when one fails then use different culture and try second one. Or change period/comma to other one using string method.
– jdweng
Jan 3 at 12:31
1
You can try thisdecimal.TryParse(stringNumber, NumberStyles.Any, CultureInfo.InvariantCulture out decimal val)
.CultureInfo.InvariantCulture
will take care of the decimal character, whatever it is.
– ikerbera
Jan 3 at 12:53
|
show 4 more comments
4
What culture is your PC set to? Does it use a period for decimal point?
– jdweng
Jan 3 at 12:13
I'm german. I want to use "." or "," for decimal points. Either one is fine. But right now I can't enter either of them. What I can do is: type: "1" then use the left arrow key and write a ".", the program will automatically put a 0 in front. It now shows "0.1" and it works. But thats not very intuitive...
– jan97con
Jan 3 at 12:17
1
your problem is using UpdateSourceTrigger=PropertyChanged. It update the source after pressing each key. If you put the point character, it update the source with string value "0." what give you back "0" without point. You can change UpdateSourceTrigger to Default and update Source manually when you press Enter.
– Stefan W.
Jan 3 at 12:26
One should work and the other fail. Not both fail. If you want bot the work then us TryParse and when one fails then use different culture and try second one. Or change period/comma to other one using string method.
– jdweng
Jan 3 at 12:31
1
You can try thisdecimal.TryParse(stringNumber, NumberStyles.Any, CultureInfo.InvariantCulture out decimal val)
.CultureInfo.InvariantCulture
will take care of the decimal character, whatever it is.
– ikerbera
Jan 3 at 12:53
4
4
What culture is your PC set to? Does it use a period for decimal point?
– jdweng
Jan 3 at 12:13
What culture is your PC set to? Does it use a period for decimal point?
– jdweng
Jan 3 at 12:13
I'm german. I want to use "." or "," for decimal points. Either one is fine. But right now I can't enter either of them. What I can do is: type: "1" then use the left arrow key and write a ".", the program will automatically put a 0 in front. It now shows "0.1" and it works. But thats not very intuitive...
– jan97con
Jan 3 at 12:17
I'm german. I want to use "." or "," for decimal points. Either one is fine. But right now I can't enter either of them. What I can do is: type: "1" then use the left arrow key and write a ".", the program will automatically put a 0 in front. It now shows "0.1" and it works. But thats not very intuitive...
– jan97con
Jan 3 at 12:17
1
1
your problem is using UpdateSourceTrigger=PropertyChanged. It update the source after pressing each key. If you put the point character, it update the source with string value "0." what give you back "0" without point. You can change UpdateSourceTrigger to Default and update Source manually when you press Enter.
– Stefan W.
Jan 3 at 12:26
your problem is using UpdateSourceTrigger=PropertyChanged. It update the source after pressing each key. If you put the point character, it update the source with string value "0." what give you back "0" without point. You can change UpdateSourceTrigger to Default and update Source manually when you press Enter.
– Stefan W.
Jan 3 at 12:26
One should work and the other fail. Not both fail. If you want bot the work then us TryParse and when one fails then use different culture and try second one. Or change period/comma to other one using string method.
– jdweng
Jan 3 at 12:31
One should work and the other fail. Not both fail. If you want bot the work then us TryParse and when one fails then use different culture and try second one. Or change period/comma to other one using string method.
– jdweng
Jan 3 at 12:31
1
1
You can try this
decimal.TryParse(stringNumber, NumberStyles.Any, CultureInfo.InvariantCulture out decimal val)
. CultureInfo.InvariantCulture
will take care of the decimal character, whatever it is.– ikerbera
Jan 3 at 12:53
You can try this
decimal.TryParse(stringNumber, NumberStyles.Any, CultureInfo.InvariantCulture out decimal val)
. CultureInfo.InvariantCulture
will take care of the decimal character, whatever it is.– ikerbera
Jan 3 at 12:53
|
show 4 more comments
1 Answer
1
active
oldest
votes
You don't need StringToDoubleConverter.
The problem is using UpdateSourceTrigger=PropertyChanged. It update the source after pressing each key. If you put the point character, it update the source with string value "0." what give you back "0" without point. You can change UpdateSourceTrigger to Default and update Source manually when you press Enter with KeyDown event
<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DelayModel.DelayTime}" KeyDown="TextBox_KeyDown"/>
and code behind
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
(sender as TextBox).GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
Thanks Stefan! The Key.Enter-Method was really helpful.
– jan97con
Jan 4 at 8:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022040%2fstring-to-double-converter-with-ivalueconverter-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You don't need StringToDoubleConverter.
The problem is using UpdateSourceTrigger=PropertyChanged. It update the source after pressing each key. If you put the point character, it update the source with string value "0." what give you back "0" without point. You can change UpdateSourceTrigger to Default and update Source manually when you press Enter with KeyDown event
<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DelayModel.DelayTime}" KeyDown="TextBox_KeyDown"/>
and code behind
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
(sender as TextBox).GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
Thanks Stefan! The Key.Enter-Method was really helpful.
– jan97con
Jan 4 at 8:35
add a comment |
You don't need StringToDoubleConverter.
The problem is using UpdateSourceTrigger=PropertyChanged. It update the source after pressing each key. If you put the point character, it update the source with string value "0." what give you back "0" without point. You can change UpdateSourceTrigger to Default and update Source manually when you press Enter with KeyDown event
<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DelayModel.DelayTime}" KeyDown="TextBox_KeyDown"/>
and code behind
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
(sender as TextBox).GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
Thanks Stefan! The Key.Enter-Method was really helpful.
– jan97con
Jan 4 at 8:35
add a comment |
You don't need StringToDoubleConverter.
The problem is using UpdateSourceTrigger=PropertyChanged. It update the source after pressing each key. If you put the point character, it update the source with string value "0." what give you back "0" without point. You can change UpdateSourceTrigger to Default and update Source manually when you press Enter with KeyDown event
<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DelayModel.DelayTime}" KeyDown="TextBox_KeyDown"/>
and code behind
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
(sender as TextBox).GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
You don't need StringToDoubleConverter.
The problem is using UpdateSourceTrigger=PropertyChanged. It update the source after pressing each key. If you put the point character, it update the source with string value "0." what give you back "0" without point. You can change UpdateSourceTrigger to Default and update Source manually when you press Enter with KeyDown event
<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DelayModel.DelayTime}" KeyDown="TextBox_KeyDown"/>
and code behind
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
(sender as TextBox).GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
answered Jan 3 at 13:46
Stefan W.Stefan W.
1869
1869
Thanks Stefan! The Key.Enter-Method was really helpful.
– jan97con
Jan 4 at 8:35
add a comment |
Thanks Stefan! The Key.Enter-Method was really helpful.
– jan97con
Jan 4 at 8:35
Thanks Stefan! The Key.Enter-Method was really helpful.
– jan97con
Jan 4 at 8:35
Thanks Stefan! The Key.Enter-Method was really helpful.
– jan97con
Jan 4 at 8:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022040%2fstring-to-double-converter-with-ivalueconverter-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
What culture is your PC set to? Does it use a period for decimal point?
– jdweng
Jan 3 at 12:13
I'm german. I want to use "." or "," for decimal points. Either one is fine. But right now I can't enter either of them. What I can do is: type: "1" then use the left arrow key and write a ".", the program will automatically put a 0 in front. It now shows "0.1" and it works. But thats not very intuitive...
– jan97con
Jan 3 at 12:17
1
your problem is using UpdateSourceTrigger=PropertyChanged. It update the source after pressing each key. If you put the point character, it update the source with string value "0." what give you back "0" without point. You can change UpdateSourceTrigger to Default and update Source manually when you press Enter.
– Stefan W.
Jan 3 at 12:26
One should work and the other fail. Not both fail. If you want bot the work then us TryParse and when one fails then use different culture and try second one. Or change period/comma to other one using string method.
– jdweng
Jan 3 at 12:31
1
You can try this
decimal.TryParse(stringNumber, NumberStyles.Any, CultureInfo.InvariantCulture out decimal val)
.CultureInfo.InvariantCulture
will take care of the decimal character, whatever it is.– ikerbera
Jan 3 at 12:53