Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6588865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:06:05+00:00 2026-05-25T17:06:05+00:00

I have created a user control like numeric updown as follows <StackPanel Orientation=Horizontal VerticalAlignment=Top

  • 0

I have created a user control like numeric updown as follows

 <StackPanel Orientation="Horizontal" VerticalAlignment="Top" >
    <TextBox x:Name="InputTextBox" Grid.Row="0" Grid.Column="0" Grid.RowSpan="1"
                Style="{StaticResource NumericUpDownTextBoxStyle}"

             KeyDown="InputTextBox_KeyDown" 
             KeyUp="InputTextBox_KeyUp"
             GotFocus="InputTextBox_GotFocus" 
             LostFocus="InputTextBox_LostFocus"  
             MouseWheel="InputTextBox_MouseWheel"
             MouseEnter="InputTextBox_MouseEnter"
             LayoutUpdated="InputTextBox_LayoutUpdated" 
             Text="{Binding Path=ControlValue, Mode=TwoWay,ValidatesOnDataErrors=True,ValidatesOnExceptions=True,NotifyOnValidationError=True}"/>
</StackPanel>

I have bind a ViewModel to this control where I Set ControlValue property to TextBox property of the user control template textbox.

Everthing works fine at a control level. I have exposed from usercontrol.

public static readonly DependencyProperty MaximumValueProperty;

public static readonly DependencyProperty MinimumValueProperty;

public static readonly DependencyProperty StepValueProperty;

public static readonly DependencyProperty TextValueProperty;

My Properties are

 public double Maximum
{
    get
    {
        return (double)GetValue(MaximumValueProperty);
    }
    set
    {
        SetValue(MaximumValueProperty, value);
        this.ViewModel.Maximum = this.Maximum;
    }
}

public double Minimum
{
    get
    {
        return (double)GetValue(MinimumValueProperty);
    }
    set
    {
        SetValue(MinimumValueProperty, value);
        this.ViewModel.Minimum = this.Minimum;
    }
}

public double Step
{
    get
    {
        return (double)GetValue(StepValueProperty);
    }
    set
    {
        SetValue(StepValueProperty, value);
        this.ViewModel.Step = this.Step;
    }
}

public double TextValue
{
    get
    {
        return (double)GetValue(TextValueProperty);
    }
    set
    {
        SetValue(TextValueProperty, value);
        this.ViewModel.ControlValue = Convert.ToString(value);
    }
}

Initialization of the property.

  static NumericUpDown()
    {
        MaximumValueProperty = DependencyProperty.Register("Maximum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(null));
        MinimumValueProperty = DependencyProperty.Register("Minimum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(null));
        StepValueProperty = DependencyProperty.Register("Step", typeof(double), typeof(NumericUpDown), new PropertyMetadata(null));
        TextValueProperty = DependencyProperty.Register("TextValue", typeof(double), typeof(NumericUpDown), new PropertyMetadata(null));
    }

My Usercontrol implementation in the MainPage.xaml page as follows

 <local:NumericUpDown Maximum="28" Minimum="-28" Step="0.25" TextValue="{Binding ElementName=FranePrice, Path=DataContext.FranePrice}"></local:NumericUpDown>

Where I have another ViewModel which i bind to the XAML page and there is a Property in the ViewModel which i bind to the TextValue property of the Usercontrol.

FramePrice is property in the View model that i bind to the TextValue property of the user control

and Main page XAML is

<UserControl x:Class="DatePicker.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DatePicker"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

<Grid x:Name="LayoutRoot" Background="White">
    <StackPanel Orientation="Vertical">

        <local:NumericUpDown Maximum="28" Minimum="-28" Step="0.25" TextValue="{Binding ElementName=FranePrice, Path=DataContext.FranePrice}"></local:NumericUpDown>
        <Button Content="Show Date" Height="23" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click"/>
    </StackPanel>
</Grid>

This View model of the page where i used user control. On click event i showing TextValue to user.

public class MainPageViewModel : EntityViewModel
{

    public MainPageViewModel()
    {

    }
    private double framePrice;
    public Double FramePrice
    {
        get
        {
            return framePrice;
        }
        set
        {
            framePrice = value;
            PropertyChangedHandler("FramePrice");
        }
    }
}

When I change the TextValue in the User control it doesnot change in the FramePrice property of the page viewmodel.

Is anything wrong in the code.???

As per Luke Woodward‘s post I have updated code as follows

 public static readonly DependencyProperty MaximumValueProperty;
    public static readonly DependencyProperty MinimumValueProperty;
    public static readonly DependencyProperty StepValueProperty;
    public static readonly DependencyProperty TextValueProperty;

    public static double Max;
    public static double Min;
    public static double Stp;
    public static double Val;

  public double Maximum
    {
        get
        {
            return (double)GetValue(MaximumValueProperty);
        }
        set
        {
            SetValue(MaximumValueProperty, value);
        }
    }

    public double Minimum
    {
        get
        {
            return (double)GetValue(MinimumValueProperty);
        }
        set
        {
            SetValue(MinimumValueProperty, value);
        }
    }

    public double Step
    {
        get
        {
            return (double)GetValue(StepValueProperty);
        }
        set
        {
            SetValue(StepValueProperty, value);
        }
    }

    public double TextValue
    {
        get
        {
            return (double)GetValue(TextValueProperty);
        }
        set
        {
            SetValue(TextValueProperty, value);
        }
    }
  static NumericUpDown()
    {
        MaximumValueProperty = DependencyProperty.Register("Maximum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(new PropertyChangedCallback(onMaximumValueChanged)));
        MinimumValueProperty = DependencyProperty.Register("Minimum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(new PropertyChangedCallback(onMinimumValueChanged)));
        StepValueProperty = DependencyProperty.Register("Step", typeof(double), typeof(NumericUpDown), new PropertyMetadata(new PropertyChangedCallback(onStepValueChanged)));
        TextValueProperty = DependencyProperty.Register("TextValue", typeof(double), typeof(NumericUpDown), new PropertyMetadata(new PropertyChangedCallback(onTextValueChanged)));
    }

    private static void onStepValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Stp = (double)e.NewValue;
    }
    private static void onMinimumValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Min = (double)e.NewValue;
    }
    private static void onMaximumValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Max = (double)e.NewValue;
    }
    private static void onTextValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Val = (double)e.NewValue;
    }

Then i accessed Max, Min , Stp and Val property in user control’s view model to perform my logic.

and XAML code is follows

<local:NumericUpDown x:Name="ctlUpDown" Maximum="28" Minimum="-28" Step="0.25" TextValue="{Binding Path=FramePrice}"></local:NumericUpDown>

and XAML of user control

<StackPanel Margin="5" Orientation="Horizontal" VerticalAlignment="Center">
    <TextBox x:Name="InputTextBox" Grid.Row="0" Grid.Column="0" Grid.RowSpan="1"
             Height="23" VerticalAlignment="Top" 
             Width="50" TextAlignment="Center"  
             KeyDown="InputTextBox_KeyDown" 
             KeyUp="InputTextBox_KeyUp"
             GotFocus="InputTextBox_GotFocus" 
             LostFocus="InputTextBox_LostFocus"  
             MouseWheel="InputTextBox_MouseWheel"
             MouseEnter="InputTextBox_MouseEnter"
             Text="{Binding Path=TextValue, ElementName=ctlUpDown,  Mode=TwoWay,ValidatesOnDataErrors=True,ValidatesOnExceptions=True,NotifyOnValidationError=True}"
             />
</StackPanel>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:06:05+00:00Added an answer on May 25, 2026 at 5:06 pm

    The first thing I noticed wrong about your code was the properties Maximum, Minimum, Step and TextValue. Here’s the TextValue property:

    public double TextValue
    {
        get
        {
            return (double)GetValue(TextValueProperty);
        }
        set
        {
            SetValue(TextValueProperty, value);
            this.ViewModel.ControlValue = Convert.ToString(value);
        }
    }
    

    Properties that are backed by a dependency property, such as the four I mentioned above, should ALWAYS look like the following:

    public double TextValue
    {
        get { return (double)GetValue(TextValueProperty); }
        set { SetValue(TextValueProperty, value); }
    }
    

    In other words, the getter should contain nothing more than a call to GetValue, and the setter should contain nothing more than a call to SetValue.

    The reason for this is that when Silverlight changes the value of the TextValue dependency property, it won’t do it by using the property above. The values of dependency properties are stored within the Silverlight dependency system, and when Silverlight wants to change the value of one of them, it goes directly to this dependency system. It doesn’t call your code at all. Properties like that above are provided only for your convenience, giving you an easy way to access and change the value stored in the dependency property. They will never be called by anything other than your own code.

    Generally, if you want a method to be called whenever a dependency property value changes, you need to pass a PropertyChangedCallback in the PropertyMetadata when registering the dependency property. However, I suspect that in your case you won’t need to do that.

    It seems to me that you have three properties:

    • the FramePrice property in your view-model class,
    • the TextValue dependency property of your NumericUpDown user control,
    • the Text dependency property of the TextBox within your NumericUpDown user control’s XAML.

    My impression is that you want the FramePrice property in your view-model to always have the same value as the Text property of the TextBox. To do that, you need to bind the FramePrice property to the NumericUpDown’s TextValue property, and then bind that to the Text property of the TextBox.

    To bind the first two of these properties together, there are a couple of things to change. Firstly, the TextValue property in your <local:NumericUpDown> element should look like

    TextValue="{Binding Path=FramePrice}"
    

    The binding {Binding ElementName=FramePrice, Path=DataContext.FramePrice} won’t work, because there’s no element in your XAML with the attribute x:Name="FramePrice". The value of an ElementName property in a {Binding ...} must match the x:Name of an object in the XAML.

    You also need to set up the DataContext for your main page. If your main page view-model object has a zero-argument constructor, one way of doing this is to follow this answer.

    To bind the second two properties together, I would:

    • add an x:Name attribute to the <UserControl> element of your NumericUpDown control (x:Name="ctlUpDown", say),
    • replace the Text property of the TextBox within your NumericUpDown control with the following:

      Text="{Binding Path=TextValue, ElementName=ctlUpDown, Mode=TwoWay, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, NotifyOnValidationError=True}"/>
      

    Once you’ve done that, you can then remove all of the lines this.ViewModel.SomeProperty = ... from your code-behind class. They’re not necessary, and as I’ve already explained they won’t be run when you wanted them to.

    Finally, is there a reason you’re not using the Silverlight Toolkit’s NumericUpDown control?

    EDIT 2: Against my better judgement I took a look at one of the two Silverlight projects you uploaded (I ignored the one with _2 in it). It bears very little resemblance to your question.

    I can only assume you want the two textboxes (one of which is in a user control) to always have the same value. I was able to do this after making the following changes:

    • MainPageViewModel.cs: add ClearErrorFromProperty("DPropertyBind"); to the property setter. (Otherwise the validation error never gets cleared.)

    • MyUserControlWVM.xaml: removed reference to LostFocus event handler, added binding on Text property and added add x:Name attribute to the <UserControl> element. In other words, it now looks like the following:

      <UserControl x:Class="DependencyPropertyBinding.MyUserControlWVM"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          mc:Ignorable="d"
          x:Name="ctlWVM"
          d:DesignHeight="300" d:DesignWidth="205">
          <StackPanel Orientation="Horizontal" Width="204" Height="32">
              <TextBox x:Name="textbox" Height="30" Width="200" Text="{Binding Path=DProperty, ElementName=ctlWVM, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />
          </StackPanel>
      </UserControl>
      
    • MyUserControlWVM.xaml.cs: renamed dependency property DependencyPropertyValue to DPropertyProperty (the naming convention is that the static readonly field has the name of the property (in this case DProperty) with Property appended). I also removed the TextBox_LostFocus event handler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created successfully a wpf user control inheritance like this: <base:BaseUserControl x:Class=wpfcontrolinheritance.InheritedUserControl xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
I have created a user control to handle adding comments to certain business entities,
I have created a user control (CheckedDirTree) that exposes a CheckedFolder property which in
I have created a COM enabled .NET User Control and am wondering what is
I have a user control that I've created, however when I go to add
We have created a User Control for ASP.Net. At any one time, a user's
I have an AJAX-enabled SharePoint 2007 site. I have also created a user control
I have a listview which I would like to fill with self created user
I am new to WPF and have created a WPF User Control Library I
I would like to show my user control inside row details. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.