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

  • Home
  • SEARCH
  • 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 996393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:52:12+00:00 2026-05-16T06:52:12+00:00

I’ve got a custom control ive created that is instanced in my view, in

  • 0

I’ve got a custom control ive created that is instanced in my view, in my custom control (a text box) i have a dependency property set up which is bound to the text value, this all works correctly and updates. However I now want to bind a dependency property of my view to the dependency property of my control but I cant get this to quite work. Heres what i mean

Control selected bits

    <TextBox x:Name="txtBox" 
                     GotFocus="txtBox_GotFocus" 
                     LostFocus="txtBox_LostFocus" 
                     Grid.Row="0" 
                     Text="{Binding TextValueProperty, Mode=TwoWay}"/>

 public DependencyProperty TextValueProperty{ get; set; }

 public int TextValue
        {
            get { return (int)GetValue(TextValueProperty); }
            set
            {
                if ((value >= MinValue) && (value <= MaxValue))
                {
                    SetValue(TextValueProperty, value);
                }
            }
        }
    public CustomControl()
    {
         TextValueProperty = DependencyProperty.Register("TextValueProperty ", typeof(int), typeof(CustomControl), new PropertyMetadata(null ));
    }

View selected bits

<my:CustomControl x:Name="TxtBoxInt"  Grid.RowSpan="2" Grid.Row="0"  Grid.Column="1"/>

I want to do this:

<my:CustomControlx:Name="TxtBoxInt" TextValueProperty="{Binding DestinationProperty}" Grid.RowSpan="2" Grid.Row="0"  Grid.Column="1"/>

and Code from view

 public DependencyProperty DestionationProperty;


        public int Destination
        {
            get
            {
                return (int)GetValue(DestionationProperty);
            }
            set
            {
                SetValue(DestionationProperty, value);
            }
        }

        public CustomControl()
        {
            DestionationProperty = DependencyProperty.Register("DestionationProperty", typeof(int), typeof(CustomControl), null);            
            InitializeComponent();
        }

So what do i need to do to be able to bind my int value Destination in my view to have the same value as my TextValue from my custom control? Thanks 🙂

ps hi ash

  • 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-16T06:52:13+00:00Added an answer on May 16, 2026 at 6:52 am

    Righty oh, I have actually now sorted this problem and for the benefit of others I’ll try and note down how I did the best I can without my code in front of me.

    Okay so first of all my custom control, I wasnt utilising Dependency properties properly (haha) to begin with. So lets rectify that first.

     public partial class CustomControl : UserControl
        {
            public DependencyProperty FieldValueProperty { get; set; }
    
            public CustomControlViewModel Context = new CustomControlViewModel();
    
            public int FieldValue
            {
                get { return (int) GetValue(FieldValueProperty); }
                set
                {
                    SetValue(FieldValueProperty,value);
                }
            }
    
            public CustomControl()
            {
                this.DataContext = Context;
                FieldValueProperty = DependencyProperty.Register("FieldValue", typeof (int), typeof (CustomControl),
                                                                 new PropertyMetadata(FieldValueChanged));
                InitializeComponent();
    
            }
    
            void FieldValueChanged(Object sender, DependencyPropertyChangedEventArgs e)
            {
                Context.FieldValue = (int)e.NewValue;
            }
        }
    
        public class CustomControlViewModel : BaseClass
        {
            private int _fieldValue;
    
            public int FieldValue
            {
                get { return _fieldValue; }
                set
            { 
                _fieldValue = value;
                RaisePropertyChanged("FieldValue");
            }
            }
        }
    

    Quite a few changes there, best way to think of it is to look at the public FieldValue thats part of CustomControl which uses GetValue and SetValue as simply as methods for your dependency property, when you use SetValue it uses the dependency property, when the value changes the dependency property calls the FieldValueChanged method, which you specified it to call in the DependencyProperty.Register metadata. This method that handles the FieldValue being changed then sets the FieldValue in the CustomControlViewModel the set for this public int sets the private value that we access to return the fieldvalue but as you notice also calls the RaisePropertyChanged(“FieldValue”) method. Now this method is inherited from my BaseClass which implements this method, it basically implements the INotifyPropertyChanged stuff. So now when my value changes basically an event is pushed and now all i need to do is handle that event in my view which contains this Control. This bit is a simple bit in the Constuctor

    CustomControlInstance.Context.PropertyChanged += FieldValueChanged(Object Sender, PropertyChangedEventArgs e);
    

    Then a method for that simply

    void FieldValueChanged(Object Sender, PropertyChangedEventsArgs e)
    {
        this.FieldValue = (int)e.NewValue;
    }
    

    That then updates my FieldValue of the view with the FieldValue from the control, thats got some holes in it but its the best I can remember/explain it to you, hope this helps people, I certainly understand Dependency Properties a lot better now 🙂

    I’ll accept this answer as the accepted answer when I’m back on computer tomorrow

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
i got an object with contents of html markup in it, for example: string
I have just tried to save a simple *.rtf file with some websites and
I have a JSP page retrieving data and when single or double quotes are
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.