Binding to NumberOfDecimals doesn't work correctly

4 posts, 0 answers
  1. Harald Braunstein
    Harald Braunstein avatar
    11 posts
    Member since:
    Sep 2006

    Posted 16 Mar 2011 Link to this post

    Hello,
    I have one problem with bindings in NumericUpDown editor. Code first:

    Xaml:
    <UserControl x:Class="SilverlightApplication.MainPage"
        xmlns:telerik_pres="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input" >
      
        <Grid x:Name="LayoutRoot" Background="White">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
      
            <StackPanel x:Name="SpDecimal" Grid.Row="0">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Row="0" Grid.Column="0" Text="Default value" />
                    <telerik_pres:RadNumericUpDown x:Name="DpDecimal" 
                                               Value="{Binding Path=DefaultValue, Mode=TwoWay}"                     
                                               NumberDecimalDigits="{Binding ElementName=RnudDecimalScale, Path=Value}" 
                                               Minimum="{Binding ElementName=RnudDecimalMin, Path=Value}" 
                                               Maximum="{Binding ElementName=RnudDecimalMax, Path=Value}" 
                                               Grid.Row="0" Grid.Column="1" />
                </Grid>
            </StackPanel>
            <StackPanel x:Name="SpDecimalProp" Grid.Row="1">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
      
                    <TextBlock Grid.Row="0" Grid.Column="0" Text="Precision" />
                    <telerik_pres:RadNumericUpDown x:Name="RnudDecimalPrecision" Value="{Binding Path=Precision, Mode=TwoWay}"  Grid.Row="0" Grid.Column="1" NumberDecimalDigits="0"/>
      
                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Scale"  />
                    <telerik_pres:RadNumericUpDown x:Name="RnudDecimalScale" Value="{Binding Path=Scale, Mode=TwoWay}" Grid.Row="1" Grid.Column="1" NumberDecimalDigits="0"/>
      
                    <TextBlock Grid.Row="2" Grid.Column="0" Text="Min" />
                    <telerik_pres:RadNumericUpDown x:Name="RnudDecimalMin" Value="{Binding Path=Min, Mode=TwoWay}" Grid.Row="2" Grid.Column="1" NumberDecimalDigits="0"/>
      
                    <TextBlock Grid.Row="3" Grid.Column="0" Text="Max" />
                    <telerik_pres:RadNumericUpDown x:Name="RnudDecimalMax" Value="{Binding Path=Max, Mode=TwoWay}" Grid.Row="3" Grid.Column="1" NumberDecimalDigits="0"/>
                </Grid>
            </StackPanel>
            <StackPanel Grid.Row="2" >
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
      
                    <Button Content="Test" Click="Button_Click" Grid.Row="0" Grid.Column="0"/>
      
                    <TextBlock Text="Entity value" Grid.Row="0" Grid.Column="1"/>
                    <TextBox x:Name="TxbResult" Text="empty" Grid.Row="0" Grid.Column="1"/>
      
                </Grid>
            </StackPanel>
        </Grid>
    </UserControl>

    Code behind:
    using System.Windows;
    using System.Windows.Controls;
      
    namespace SilverlightApplication
    {
        public partial class MainPage : UserControl
        {
            Entity _entity;
      
            public MainPage()
            {
                InitializeComponent();
                this.DataContext = _entity = new Entity();
            }
      
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                TxbResult.Text = _entity.DefaultValue;
            }
        }
    }

    Entity.cs:
    using System.ComponentModel;
      
    namespace SilverlightApplication
    {
        public class Entity : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
      
            private string _defaultValue;
      
            public int Precision { get; set; }
            public int Scale { get; set; }
            public int Min { get; set; }
            public int Max { get; set; }
      
            public virtual string DefaultValue
            {
                get { return _defaultValue; }
                set
                {
                    _defaultValue = value;
                    InvokePropertyChanged("DefaultValue");
                }
            }
      
            public void InvokePropertyChanged(string changedPropertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                    handler(this, new PropertyChangedEventArgs(changedPropertyName));
            }
        }
    }

    I'm trying to bind control DpDecimal to DefaultValue of an entity. I have also some properties of the default value like precision, scale,..
    The control DpDecimal has bound its properties NumberDecimalDigits, Minimum, Maximum to controls of these properties. I have also one help button here and when click to this button the actual value of entity default value is filled into the textbox next to it, just for testing.

    I'm trying this scenario:

    1. fill min = 1, max = 1000, scale = 2, precision = 5 and default value = 500

    2. change min value to 600 and click the button test before losing focus. This works and default value is changed in control and also in entity property to min value = 600.
    3. change default value to 600,19

    4. change scale to 1 and click the test button before losing  focus. Now the value in control is changed correctly to 600,2 but the value in the entity is still 600,19, what you can see in the result textbox next to the button.

    I need somehow to fix this to have changed also the value of the property in the entity. I also need to keep default value as a string because of the structure of my application. I did try to handle events from property controls and update the control of default value source explicitly but it also doesn't work. Neither change of UpdateEvent to PropertyChanged help.

    Thanks for any advice.
  2. Yana
    Admin
    Yana avatar
    4554 posts

    Posted 22 Mar 2011 Link to this post

    Hello Harald,

    Actually this is the intended behavior of RadNumericUpDown - the scale doesn't really change the value, only its display in the NumericUpDown textbox.  You should manually round the value, for example in its setter in Entity class.

    Hope this helps.

    Regards,
    Yana
    the Telerik team
  3. DevCraft banner
  4. John Galt
    John Galt avatar
    3 posts
    Member since:
    May 2012

    Posted 26 Oct 2012 Link to this post

    Hi there,

    What if you are using EF Database first and the setter is auto-generated?

    Thanks!
  5. George
    Admin
    George avatar
    1332 posts

    Posted 31 Oct 2012 Link to this post

    Hello Joe,

     
    In this case I would suggest using a value converter for the binding so the value can be modified.

    Hope this helps.

    Kind regards,
    George
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top