How do I make it so that when lowest value is saved, if the user doesn't change it?

8 posts, 1 answers
  1. Rod
    Rod avatar
    71 posts
    Member since:
    Jan 2011

    Posted 01 Nov 2013 Link to this post

    I'm working on a new WPF application, and am making extensive use of the RadMaskedNumericInput control. I've defined a couple of validation classes based upon the ValidationRule class. I'm using this in every instance where I use a RadMaskedNumericInput control. I want it such that if the RadMaskedNumericInput control shows up and the user does nothing with it, then the lowest value is saved back to the database. This isn't happening. What's happening is a null is being saved to the database. Now, I know that I could define a default value in the table definition, and perhaps that's what I'll have to do, but I'd like to know if there's a way to make the RadMaskedNumericInput control automatically save the lowest value (in all my cases that happens to be a 0, but it could technically be any numeric value) to the backend database. You see, it's weird because the RadMaskedNumericInput control is showing a 0 in it, when it comes up, but I've found that in testing if I do nothing with that control, or just tab to it and then past it, a null is what's saved to the database. That's counterintuitive.
  2. Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1182 posts

    Posted 06 Nov 2013 Link to this post

    Hello Rod,

    Please note that any logic for saving information into your data base is part of your application and you as a developer should implement it. The RadMaskedNumericInput is not capable of accessing your custom database or detecting the lowest value entered.

    You can achieve your requirement by implementing MVVM approach in your application and binding the Value property of the control to property exposed by your business object. By following this approach you will be able to implement custom logic for saving the lowest value into your data base.

    I hope this information is helpful.

    Regards,
    Pavel R. Pavlov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Rod
    Rod avatar
    71 posts
    Member since:
    Jan 2011

    Posted 06 Nov 2013 Link to this post

    Hello Pavel,

    I am using data binding to the RadMaskedNumericInput's Value property:

    <telerik:RadMaskedNumericInput Mask="###"
                                   Margin="3,0,0,0"
                                   VerticalAlignment="Top"
                                   Grid.Column="1">
        <telerik:RadMaskedNumericInput.Value>
            <Binding Path="HowManyAlcoholDTs">
                <Binding.ValidationRules>
                    <local:ShortValidation Minimum="0" Maximum="999" ErrorMessage="Value must be between 0 and 999." />
                </Binding.ValidationRules>
            </Binding>
        </telerik:RadMaskedNumericInput.Value>
    </telerik:RadMaskedNumericInput>

    However, we're not using MVVM. Instead we're using code-behind for our code, that and CollectionViewSources for binding the data. I've got a follow-up question for you, are you saying that only following the MVVM pattern can we properly handle making sure the lowest value is saved to the database? That we can't do that in code-behind?
  5. Answer
    Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 11 Nov 2013 Link to this post

    Hi Rod,

     We think we managed to understand completely your requirement. Our previous point was that validation should be usually implemented in the ViewModels, however, of course in WPF everything is possible in code behind too.
    RadMaskedNumericInput's Value property is of type double? which means it accepts null values. To avoid them you can set:

    <telerik:RadMaskedNumericInput Mask="###"
                                      Margin="3,0,0,0"
                                      VerticalAlignment="Top" x:Name="input"
                                      maskedInput:MaskedInputExtensions.AllowNull="False"
    where
    xmlns:maskedInput="clr-namespace:Telerik.Windows.Controls.MaskedInput;assembly=Telerik.Windows.Controls.Input"
    This way the clear button won't produce null Value. On the other hand, you can bind to double property from the ViewModel but not double? property.
    Instead of your custom Validation, you can use the MaskedInputExtensions.MInimum and Maximum properties but have in mind the following - initially the ViewModel's value won't be synced - for example the control will show 10 if the MInimum is set to 10, but the VM's Value won't be updated. However, in code, if the user is about to submit the changes to the DB, you can update all ViewModels:
    private void Button_Click(object sender, RoutedEventArgs e)
           {
               var expr = this.input.GetBindingExpression(RadMaskedNumericInput.ValueProperty);
               if (expr != null)
               {
                   expr.UpdateSource();
               }
           }
    You can find this implemented in the attached project. Just press the button and the ViewModel's value will be updated.
    Please let us know if this helps you proceed further. Regards,
    Petar Mladenov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  6. Rod
    Rod avatar
    71 posts
    Member since:
    Jan 2011

    Posted 18 Nov 2013 Link to this post

    Hi Petar.

    Thank you for your response from the 11th. I'm sorry I haven't been able to view it until this moment. I've extracted everything and put it into a folder on my D: drive. However, when I opened it with VS 2012 I get the following error message:

    "D:\Work\WPF_Scrum\Current\Controls\Input\Input_WPF.csproj : error : The project file could not be loaded. Could not find a part of the path 'D:\Work\WPF_Scrum\Current\Controls\Input\Input_WPF.csproj'. D:\Work\WPF_Scrum\Current\Controls\Input\Input_WPF.csproj"

    I don't have a D:\Work\WPF_Scrum\Current\Controls\Input folder.

    I'll look over your XAML to see if I get what you're trying to do and attempt to replicate it into my project.
  7. Rod
    Rod avatar
    71 posts
    Member since:
    Jan 2011

    Posted 18 Nov 2013 Link to this post

    Hi Petar,

    I've tried the code that you gave me back on the 11th, but am afraid that it didn't work. This is what I now have:

    <DataGridTemplateColumn x:Name="ageOfFirstUseColumn" Width="SizeToHeader">
        <DataGridTemplateColumn.Header>
            <StackPanel>
                <TextBlock HorizontalAlignment="Center">Age of</TextBlock>
                <TextBlock HorizontalAlignment="Center">First Use</TextBlock>
            </StackPanel>
        </DataGridTemplateColumn.Header>
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <telerik:RadMaskedNumericInput Mask="###"
                                               BorderThickness="0"
                                               HorizontalAlignment="Center"
                                               maskedInput:MaskedInputExtensions.AllowNull="False"
                                               maskedInput:MaskedInputExtensions.Minimum="0"
                                               Background="Transparent">
                    <telerik:RadMaskedNumericInput.Value>
                        <Binding Path="AgeOfFirstUse">
                            <Binding.ValidationRules>
                                <local:ByteValidation ErrorMessage="Value must be between 0 and 255" />
                            </Binding.ValidationRules>
                        </Binding>
                    </telerik:RadMaskedNumericInput.Value>
                </telerik:RadMaskedNumericInput>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>

    Unfortunately, I still get a "The value for column 'DaysUseLast30 in table ASISubstanceUse is DBNull."
  8. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 21 Nov 2013 Link to this post

    Hello Rod,

    I am not quite sure what exactly happens in your application. Why isn't your binding two-way ? When is this error thrown ? Is it possible for you to send us more from your code? Do you update the bindings in the way we suggested ? Is it possible for you to isolated this issue in a runnable sample and send it to us in a separate support thread. This way we would be better able to investigate this and advice you more precisely.
    Thank you in advance for your cooperation.

    Regards,
    Petar Mladenov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  9. Rod
    Rod avatar
    71 posts
    Member since:
    Jan 2011

    Posted 21 Nov 2013 Link to this post

    Hi Petar,

    The binding isn't two way for the dropdown/combobox, because it isn't supposed to be. It's only meant to show the user the possible, valid values.

    I've discovered that the error occurs when trying to save the data to the backend database. This application first saves the data locally (the intent is just in case the backend database isn't available), and then it saves it remotely. The problem occurs when attempting to save it remotely, as it is coming from the local database with a null. That is odd, because I would have thought that the code you gave me before (MaskedInputExtensions.AllowNull="False") would have prevented any nulls from going into the local database, but that's not true, it still is null.

    For now, I've changed the middle tier component (a WCF service) so that if the fields are null, it will put in a 0. That's working for me now.
Back to Top
UI for WPF is Visual Studio 2017 Ready