WPF DataGrid - DataGridTextColumn decimal/double/float input with PropertyChanged










0















I'm having trouble with decimal (double/float) inputs in DataGridTextColumn with UpdateSourceTrigger=PropertyChanged.



(1) I have searched several sites and many suggest to change this to LostFocus. Im not very keen on this implementation cause the behaviour with PropertyChanged is what I require. Unless there is a way to get the same result with LostFocus??



I have read the following articles on stackoverflow -
Link 1, Link 2, Link 3, Link 4, Link 5 & Link 6



(2) Using Binding="Binding StringFormat=N2"or its variation has a very annoying behavior as described in the comments in Link 4.



(3) The other method is to allow string input and parse it as double in the back end. This is fine-ish, and I'm holding onto it as a last resort solution only.



(4) Lastly in the same Link 4, a solution mentioned by xmedeko - System.Windows.FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false; for .NET 4.5 or later only.



I do have the newer version but not sure how/where to implement this line of code. Or if there's a way to turn this property false in the XAML itself? Any help is greatly appreciated. Unable to reply as a comment on that link due to my reputation level.



If there is a any other neater way while keeping the behavior similar to PropertyChanged, would be very helpful.



CODE

XAML - <DataGridTextColumn Header="Rate" Binding="Binding Path=Rate, UpdateSourceTrigger=PropertyChanged" Width="90"/>



cs -



public void NotifyPropertyChanged(string property)

PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));


public double Rate

get return rate;
set rate = value; NotifyPropertyChanged("Rate");



The code is actually very simple. This is displayed on a datagrid. The DataGridTextColumn does not accept decimal values and this correctly noted by Chevul Ervin in Link 2 -




UpdateSourceTrigger=PropertyChanged reevalutes the text on every
keystroke. A number that ends in a decimal point is invalid. Change
the UpdateSourceTrigger to LostFocus (same as removing it) or try to
type the '.' while you have other digits after it.











share|improve this question
























  • It would be better if you show your code attempts so we can check whats problem you are facing. Without code its bit difficult.

    – M. Adeel Khalid
    Nov 15 '18 at 5:45











  • I have updated the main post with the code as requested.

    – Bandook
    Nov 15 '18 at 6:44















0















I'm having trouble with decimal (double/float) inputs in DataGridTextColumn with UpdateSourceTrigger=PropertyChanged.



(1) I have searched several sites and many suggest to change this to LostFocus. Im not very keen on this implementation cause the behaviour with PropertyChanged is what I require. Unless there is a way to get the same result with LostFocus??



I have read the following articles on stackoverflow -
Link 1, Link 2, Link 3, Link 4, Link 5 & Link 6



(2) Using Binding="Binding StringFormat=N2"or its variation has a very annoying behavior as described in the comments in Link 4.



(3) The other method is to allow string input and parse it as double in the back end. This is fine-ish, and I'm holding onto it as a last resort solution only.



(4) Lastly in the same Link 4, a solution mentioned by xmedeko - System.Windows.FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false; for .NET 4.5 or later only.



I do have the newer version but not sure how/where to implement this line of code. Or if there's a way to turn this property false in the XAML itself? Any help is greatly appreciated. Unable to reply as a comment on that link due to my reputation level.



If there is a any other neater way while keeping the behavior similar to PropertyChanged, would be very helpful.



CODE

XAML - <DataGridTextColumn Header="Rate" Binding="Binding Path=Rate, UpdateSourceTrigger=PropertyChanged" Width="90"/>



cs -



public void NotifyPropertyChanged(string property)

PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));


public double Rate

get return rate;
set rate = value; NotifyPropertyChanged("Rate");



The code is actually very simple. This is displayed on a datagrid. The DataGridTextColumn does not accept decimal values and this correctly noted by Chevul Ervin in Link 2 -




UpdateSourceTrigger=PropertyChanged reevalutes the text on every
keystroke. A number that ends in a decimal point is invalid. Change
the UpdateSourceTrigger to LostFocus (same as removing it) or try to
type the '.' while you have other digits after it.











share|improve this question
























  • It would be better if you show your code attempts so we can check whats problem you are facing. Without code its bit difficult.

    – M. Adeel Khalid
    Nov 15 '18 at 5:45











  • I have updated the main post with the code as requested.

    – Bandook
    Nov 15 '18 at 6:44













0












0








0








I'm having trouble with decimal (double/float) inputs in DataGridTextColumn with UpdateSourceTrigger=PropertyChanged.



(1) I have searched several sites and many suggest to change this to LostFocus. Im not very keen on this implementation cause the behaviour with PropertyChanged is what I require. Unless there is a way to get the same result with LostFocus??



I have read the following articles on stackoverflow -
Link 1, Link 2, Link 3, Link 4, Link 5 & Link 6



(2) Using Binding="Binding StringFormat=N2"or its variation has a very annoying behavior as described in the comments in Link 4.



(3) The other method is to allow string input and parse it as double in the back end. This is fine-ish, and I'm holding onto it as a last resort solution only.



(4) Lastly in the same Link 4, a solution mentioned by xmedeko - System.Windows.FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false; for .NET 4.5 or later only.



I do have the newer version but not sure how/where to implement this line of code. Or if there's a way to turn this property false in the XAML itself? Any help is greatly appreciated. Unable to reply as a comment on that link due to my reputation level.



If there is a any other neater way while keeping the behavior similar to PropertyChanged, would be very helpful.



CODE

XAML - <DataGridTextColumn Header="Rate" Binding="Binding Path=Rate, UpdateSourceTrigger=PropertyChanged" Width="90"/>



cs -



public void NotifyPropertyChanged(string property)

PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));


public double Rate

get return rate;
set rate = value; NotifyPropertyChanged("Rate");



The code is actually very simple. This is displayed on a datagrid. The DataGridTextColumn does not accept decimal values and this correctly noted by Chevul Ervin in Link 2 -




UpdateSourceTrigger=PropertyChanged reevalutes the text on every
keystroke. A number that ends in a decimal point is invalid. Change
the UpdateSourceTrigger to LostFocus (same as removing it) or try to
type the '.' while you have other digits after it.











share|improve this question
















I'm having trouble with decimal (double/float) inputs in DataGridTextColumn with UpdateSourceTrigger=PropertyChanged.



(1) I have searched several sites and many suggest to change this to LostFocus. Im not very keen on this implementation cause the behaviour with PropertyChanged is what I require. Unless there is a way to get the same result with LostFocus??



I have read the following articles on stackoverflow -
Link 1, Link 2, Link 3, Link 4, Link 5 & Link 6



(2) Using Binding="Binding StringFormat=N2"or its variation has a very annoying behavior as described in the comments in Link 4.



(3) The other method is to allow string input and parse it as double in the back end. This is fine-ish, and I'm holding onto it as a last resort solution only.



(4) Lastly in the same Link 4, a solution mentioned by xmedeko - System.Windows.FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false; for .NET 4.5 or later only.



I do have the newer version but not sure how/where to implement this line of code. Or if there's a way to turn this property false in the XAML itself? Any help is greatly appreciated. Unable to reply as a comment on that link due to my reputation level.



If there is a any other neater way while keeping the behavior similar to PropertyChanged, would be very helpful.



CODE

XAML - <DataGridTextColumn Header="Rate" Binding="Binding Path=Rate, UpdateSourceTrigger=PropertyChanged" Width="90"/>



cs -



public void NotifyPropertyChanged(string property)

PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));


public double Rate

get return rate;
set rate = value; NotifyPropertyChanged("Rate");



The code is actually very simple. This is displayed on a datagrid. The DataGridTextColumn does not accept decimal values and this correctly noted by Chevul Ervin in Link 2 -




UpdateSourceTrigger=PropertyChanged reevalutes the text on every
keystroke. A number that ends in a decimal point is invalid. Change
the UpdateSourceTrigger to LostFocus (same as removing it) or try to
type the '.' while you have other digits after it.








c# wpf data-binding datagrid






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 6:43







Bandook

















asked Nov 15 '18 at 4:49









BandookBandook

34




34












  • It would be better if you show your code attempts so we can check whats problem you are facing. Without code its bit difficult.

    – M. Adeel Khalid
    Nov 15 '18 at 5:45











  • I have updated the main post with the code as requested.

    – Bandook
    Nov 15 '18 at 6:44

















  • It would be better if you show your code attempts so we can check whats problem you are facing. Without code its bit difficult.

    – M. Adeel Khalid
    Nov 15 '18 at 5:45











  • I have updated the main post with the code as requested.

    – Bandook
    Nov 15 '18 at 6:44
















It would be better if you show your code attempts so we can check whats problem you are facing. Without code its bit difficult.

– M. Adeel Khalid
Nov 15 '18 at 5:45





It would be better if you show your code attempts so we can check whats problem you are facing. Without code its bit difficult.

– M. Adeel Khalid
Nov 15 '18 at 5:45













I have updated the main post with the code as requested.

– Bandook
Nov 15 '18 at 6:44





I have updated the main post with the code as requested.

– Bandook
Nov 15 '18 at 6:44












1 Answer
1






active

oldest

votes


















0














I did quite a bit of research and found no solution. So going with Option 3 - allow string input and handle it in the backend.






share|improve this answer























    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%2f53312593%2fwpf-datagrid-datagridtextcolumn-decimal-double-float-input-with-propertychange%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














    I did quite a bit of research and found no solution. So going with Option 3 - allow string input and handle it in the backend.






    share|improve this answer



























      0














      I did quite a bit of research and found no solution. So going with Option 3 - allow string input and handle it in the backend.






      share|improve this answer

























        0












        0








        0







        I did quite a bit of research and found no solution. So going with Option 3 - allow string input and handle it in the backend.






        share|improve this answer













        I did quite a bit of research and found no solution. So going with Option 3 - allow string input and handle it in the backend.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 2 '18 at 6:34









        BandookBandook

        34




        34





























            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%2f53312593%2fwpf-datagrid-datagridtextcolumn-decimal-double-float-input-with-propertychange%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

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo