Trouble Inputting Doubles in View Due To Not Using Strings?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















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. Is there an easy fix?



View:



<TextBox Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" Grid.RowSpan="2" 
Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DelayModel.DelayTime, UpdateSourceTrigger=PropertyChanged}"/>


Model:



    private double _delayTime;

public double DelayTime
{
get
{
return _delayTime;
}
set
{
if (value != _delayTime)
{
_delayTime = value; NotifyPropertyChanged();
}
}
}


Another Model which uses delayTime:



public double TotalAxisTime
{
get
{
double positioningTime = 0.0;
double delayTime = 0.0;

foreach (var block in BlockList)
{
if (block is PositioningModel)
{
positioningTime = positioningTime + ((PositioningModel)block).PositioningTime;
}

if (block is DelayModel)
{
delayTime = delayTime + ((DelayModel)block).DelayTime;
}
}
return (positioningTime + delayTime);
}
}


(PositioningTime is another double value which I probably need to convert to strings.



I'm struggling to implement the Convert.ToString()-Method. Is this the right approach? Should I use dedicated Converters in XAML? Something like StringToDoubleConverter with IValueConverter?



Any advice would help. Thanks in advance.










share|improve this question


















  • 2





    Your trouble lies in the binding from double to string: whenever you edit the text in the textbox, the double value is updated and the update is reflected back the textbox. This works fine when you enter digits, say change "123" to "1234". But suppose that you then add a decimal point: "1234.". The value will just be treated as 1234 in the model and fail to pass the 'if (value != _delayTime)' check, thereby not being updated. The textbox will just keep showing the value 1234 due to the binding. I think that using a converter is your only reasonable choice.

    – JonyVol
    Jan 3 at 8:52




















0















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. Is there an easy fix?



View:



<TextBox Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" Grid.RowSpan="2" 
Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DelayModel.DelayTime, UpdateSourceTrigger=PropertyChanged}"/>


Model:



    private double _delayTime;

public double DelayTime
{
get
{
return _delayTime;
}
set
{
if (value != _delayTime)
{
_delayTime = value; NotifyPropertyChanged();
}
}
}


Another Model which uses delayTime:



public double TotalAxisTime
{
get
{
double positioningTime = 0.0;
double delayTime = 0.0;

foreach (var block in BlockList)
{
if (block is PositioningModel)
{
positioningTime = positioningTime + ((PositioningModel)block).PositioningTime;
}

if (block is DelayModel)
{
delayTime = delayTime + ((DelayModel)block).DelayTime;
}
}
return (positioningTime + delayTime);
}
}


(PositioningTime is another double value which I probably need to convert to strings.



I'm struggling to implement the Convert.ToString()-Method. Is this the right approach? Should I use dedicated Converters in XAML? Something like StringToDoubleConverter with IValueConverter?



Any advice would help. Thanks in advance.










share|improve this question


















  • 2





    Your trouble lies in the binding from double to string: whenever you edit the text in the textbox, the double value is updated and the update is reflected back the textbox. This works fine when you enter digits, say change "123" to "1234". But suppose that you then add a decimal point: "1234.". The value will just be treated as 1234 in the model and fail to pass the 'if (value != _delayTime)' check, thereby not being updated. The textbox will just keep showing the value 1234 due to the binding. I think that using a converter is your only reasonable choice.

    – JonyVol
    Jan 3 at 8:52
















0












0








0








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. Is there an easy fix?



View:



<TextBox Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" Grid.RowSpan="2" 
Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DelayModel.DelayTime, UpdateSourceTrigger=PropertyChanged}"/>


Model:



    private double _delayTime;

public double DelayTime
{
get
{
return _delayTime;
}
set
{
if (value != _delayTime)
{
_delayTime = value; NotifyPropertyChanged();
}
}
}


Another Model which uses delayTime:



public double TotalAxisTime
{
get
{
double positioningTime = 0.0;
double delayTime = 0.0;

foreach (var block in BlockList)
{
if (block is PositioningModel)
{
positioningTime = positioningTime + ((PositioningModel)block).PositioningTime;
}

if (block is DelayModel)
{
delayTime = delayTime + ((DelayModel)block).DelayTime;
}
}
return (positioningTime + delayTime);
}
}


(PositioningTime is another double value which I probably need to convert to strings.



I'm struggling to implement the Convert.ToString()-Method. Is this the right approach? Should I use dedicated Converters in XAML? Something like StringToDoubleConverter with IValueConverter?



Any advice would help. Thanks in advance.










share|improve this question














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. Is there an easy fix?



View:



<TextBox Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" Grid.RowSpan="2" 
Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DelayModel.DelayTime, UpdateSourceTrigger=PropertyChanged}"/>


Model:



    private double _delayTime;

public double DelayTime
{
get
{
return _delayTime;
}
set
{
if (value != _delayTime)
{
_delayTime = value; NotifyPropertyChanged();
}
}
}


Another Model which uses delayTime:



public double TotalAxisTime
{
get
{
double positioningTime = 0.0;
double delayTime = 0.0;

foreach (var block in BlockList)
{
if (block is PositioningModel)
{
positioningTime = positioningTime + ((PositioningModel)block).PositioningTime;
}

if (block is DelayModel)
{
delayTime = delayTime + ((DelayModel)block).DelayTime;
}
}
return (positioningTime + delayTime);
}
}


(PositioningTime is another double value which I probably need to convert to strings.



I'm struggling to implement the Convert.ToString()-Method. Is this the right approach? Should I use dedicated Converters in XAML? Something like StringToDoubleConverter with IValueConverter?



Any advice would help. Thanks in advance.







c# xaml types converters






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 8:18









jan97conjan97con

257




257








  • 2





    Your trouble lies in the binding from double to string: whenever you edit the text in the textbox, the double value is updated and the update is reflected back the textbox. This works fine when you enter digits, say change "123" to "1234". But suppose that you then add a decimal point: "1234.". The value will just be treated as 1234 in the model and fail to pass the 'if (value != _delayTime)' check, thereby not being updated. The textbox will just keep showing the value 1234 due to the binding. I think that using a converter is your only reasonable choice.

    – JonyVol
    Jan 3 at 8:52
















  • 2





    Your trouble lies in the binding from double to string: whenever you edit the text in the textbox, the double value is updated and the update is reflected back the textbox. This works fine when you enter digits, say change "123" to "1234". But suppose that you then add a decimal point: "1234.". The value will just be treated as 1234 in the model and fail to pass the 'if (value != _delayTime)' check, thereby not being updated. The textbox will just keep showing the value 1234 due to the binding. I think that using a converter is your only reasonable choice.

    – JonyVol
    Jan 3 at 8:52










2




2





Your trouble lies in the binding from double to string: whenever you edit the text in the textbox, the double value is updated and the update is reflected back the textbox. This works fine when you enter digits, say change "123" to "1234". But suppose that you then add a decimal point: "1234.". The value will just be treated as 1234 in the model and fail to pass the 'if (value != _delayTime)' check, thereby not being updated. The textbox will just keep showing the value 1234 due to the binding. I think that using a converter is your only reasonable choice.

– JonyVol
Jan 3 at 8:52







Your trouble lies in the binding from double to string: whenever you edit the text in the textbox, the double value is updated and the update is reflected back the textbox. This works fine when you enter digits, say change "123" to "1234". But suppose that you then add a decimal point: "1234.". The value will just be treated as 1234 in the model and fail to pass the 'if (value != _delayTime)' check, thereby not being updated. The textbox will just keep showing the value 1234 due to the binding. I think that using a converter is your only reasonable choice.

– JonyVol
Jan 3 at 8:52














1 Answer
1






active

oldest

votes


















0














Here's one quick way of handling this. Not the most suitable, and a converter would likely be better.



public string DelayTimeText
{
get
{
return _delayTimeText;
}
set
{
_delayTimeText = value; NotifyPropertyChanged();
}
}

public double DelayTime
{
get
{
if (double.TryParse( DelayTimeText, out double val ));
_delayTime = val;

return _delayTime;
}
}


You'd then bind to the Text property instead, and your calculations would rely on the TryParse, which would fall back to the previous value if it fails to parse.






share|improve this answer
























  • I've tried your method, but I think it's flawed. When I try to type "0.7" into my View it converts into "7". It just ignores the numbers in front of the dot...

    – jan97con
    Jan 3 at 11:31












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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018588%2ftrouble-inputting-doubles-in-view-due-to-not-using-strings%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









0














Here's one quick way of handling this. Not the most suitable, and a converter would likely be better.



public string DelayTimeText
{
get
{
return _delayTimeText;
}
set
{
_delayTimeText = value; NotifyPropertyChanged();
}
}

public double DelayTime
{
get
{
if (double.TryParse( DelayTimeText, out double val ));
_delayTime = val;

return _delayTime;
}
}


You'd then bind to the Text property instead, and your calculations would rely on the TryParse, which would fall back to the previous value if it fails to parse.






share|improve this answer
























  • I've tried your method, but I think it's flawed. When I try to type "0.7" into my View it converts into "7". It just ignores the numbers in front of the dot...

    – jan97con
    Jan 3 at 11:31
















0














Here's one quick way of handling this. Not the most suitable, and a converter would likely be better.



public string DelayTimeText
{
get
{
return _delayTimeText;
}
set
{
_delayTimeText = value; NotifyPropertyChanged();
}
}

public double DelayTime
{
get
{
if (double.TryParse( DelayTimeText, out double val ));
_delayTime = val;

return _delayTime;
}
}


You'd then bind to the Text property instead, and your calculations would rely on the TryParse, which would fall back to the previous value if it fails to parse.






share|improve this answer
























  • I've tried your method, but I think it's flawed. When I try to type "0.7" into my View it converts into "7". It just ignores the numbers in front of the dot...

    – jan97con
    Jan 3 at 11:31














0












0








0







Here's one quick way of handling this. Not the most suitable, and a converter would likely be better.



public string DelayTimeText
{
get
{
return _delayTimeText;
}
set
{
_delayTimeText = value; NotifyPropertyChanged();
}
}

public double DelayTime
{
get
{
if (double.TryParse( DelayTimeText, out double val ));
_delayTime = val;

return _delayTime;
}
}


You'd then bind to the Text property instead, and your calculations would rely on the TryParse, which would fall back to the previous value if it fails to parse.






share|improve this answer













Here's one quick way of handling this. Not the most suitable, and a converter would likely be better.



public string DelayTimeText
{
get
{
return _delayTimeText;
}
set
{
_delayTimeText = value; NotifyPropertyChanged();
}
}

public double DelayTime
{
get
{
if (double.TryParse( DelayTimeText, out double val ));
_delayTime = val;

return _delayTime;
}
}


You'd then bind to the Text property instead, and your calculations would rely on the TryParse, which would fall back to the previous value if it fails to parse.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 10:44









user3265613user3265613

12711




12711













  • I've tried your method, but I think it's flawed. When I try to type "0.7" into my View it converts into "7". It just ignores the numbers in front of the dot...

    – jan97con
    Jan 3 at 11:31



















  • I've tried your method, but I think it's flawed. When I try to type "0.7" into my View it converts into "7". It just ignores the numbers in front of the dot...

    – jan97con
    Jan 3 at 11:31

















I've tried your method, but I think it's flawed. When I try to type "0.7" into my View it converts into "7". It just ignores the numbers in front of the dot...

– jan97con
Jan 3 at 11:31





I've tried your method, but I think it's flawed. When I try to type "0.7" into my View it converts into "7". It just ignores the numbers in front of the dot...

– jan97con
Jan 3 at 11:31




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018588%2ftrouble-inputting-doubles-in-view-due-to-not-using-strings%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]