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 6162817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:43:08+00:00 2026-05-23T21:43:08+00:00

What I need to do is? I need to make one expression on which

  • 0

What I need to do is?

I need to make one expression on which dependency property should depends on.

Suppose as per below:

Count = dependOne + dependTwo;

Here, Count is Dependency property and dependOne and dependTwo are two variables on which dependency property Count should Depends on.

Now whenever I change variable dependOne or dependTwo the dependency property should have to update automatically.

Is it possible? If yes then how?

See the code below

XAML:

<Window x:Class="DependecnyProperty.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Background="LightGray" Text="{Binding Path=Count}" Height="23" HorizontalAlignment="Left" Margin="164,102,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="164,148,0,0" Name="button1" VerticalAlignment="Top" Width="120" Click="button1_Click" />
    </Grid>
</Window>

CODE BEHIND:

namespace DependecnyProperty
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;

            Count = dependOne + dependTwo;
        }

        int dependOne = 0;
        int dependTwo = 0;

        public int Count
        {
            get { return (int)GetValue(CountProperty); }
            set { SetValue(CountProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Count.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CountProperty =
            DependencyProperty.Register("Count", typeof(int), typeof(MainWindow), new UIPropertyMetadata(12));

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            dependOne = dependOne + 2;
            dependTwo = dependTwo + 1;

            //I need to find way ...now here i have changed value of two variable.
            //now it is possible to change Dependency Property
            //Without here setting the value of dependency property
        }


    }
}

EDIT UPDATED CODEBEHIND:

namespace DependecnyProperty
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ViewModel vm;
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;

            vm = new ViewModel();
            //this.DataContext = vm;
            Count = vm.DependOne + vm.DependTwo;
        }


        public int Count
        {
            get { return (int)GetValue(CountProperty); }
            set { SetValue(CountProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Count.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CountProperty =
            DependencyProperty.Register("Count", typeof(int), typeof(MainWindow), new UIPropertyMetadata(12));

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            vm.DependOne++;
            vm.DependTwo++;
            //I need to find way ...now here i have changed value of two variable.
            //now it is possible to change Dependency Property
            //Without here setting the value of dependency property

        }

        public class ViewModel : INotifyPropertyChanged
        {

            public ViewModel()
            {

            }

            private int dependOne;

            public int DependOne
            {
                get
                {
                    return dependOne;
                }
                set 
                { 
                    dependOne = value;
                    OnPropertyChanged("DependOne");
                }
            }

            private int dependTwo;

            public int DependTwo
            {
                get 
                { 
                    return dependTwo; 
                }
                set 
                { 
                    dependTwo = value;
                    OnPropertyChanged("DependTwo");
                }
            }

            #region --  INotifyPropertyChanged Members  --

            public event PropertyChangedEventHandler PropertyChanged;
            public void OnPropertyChanged(string propertyNameArg)
            {
                PropertyChangedEventHandler handler = this.PropertyChanged;

                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyNameArg));
                }
            }
            #endregion
        }
    }
}
  • 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-23T21:43:08+00:00Added an answer on May 23, 2026 at 9:43 pm

    I don’t know what is the best solution for you. One of these ,however, would be that you use Property like:

        //field
        private int _dependOne;
    
        //property
        public int DependOne
        {
            get { return _dependOne; }
            set {
                _dependOne = value;
                Count += value;
            }
        }
    
        //Finally, use the property instead of the field
        //dependOne = dependOne + 2;
        DependOne += 2;
    

    UPDATED:
    The important thing is that you don’t update ViewModel properties in Codebehind. Instead, you could call a ViewModel method.

    XAML

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Local="clr-namespace:WpfApplication1"
            Title="MainWindow" Height="350" Width="525">
    
        <StackPanel>
            <StackPanel.Resources>
                <Local:MyConverter x:Key="myConverter"/>
            </StackPanel.Resources>
            <TextBox>
                <TextBox.Text>
                    <MultiBinding Converter="{StaticResource myConverter}">
                        <Binding Path="DependOne" Mode="OneWay"/>
                        <Binding Path="DependTwo" Mode="OneWay"/>
                    </MultiBinding>
                </TextBox.Text>
            </TextBox>
            <Button Click="button1_Click">click</Button>
        </StackPanel>
    </Window>
    

    CODE

    public partial class MainWindow : Window
    {
        ViewModel vm;
        public MainWindow()
        {
            InitializeComponent();
    
            vm = new ViewModel();
            this.DataContext = vm;
        }
    
    
        //public int Count
        //{
        //    get { return (int)GetValue(CountProperty); }
        //    set { SetValue(CountProperty, value); }
        //}
    
        //// Using a DependencyProperty as the backing store for Count.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty CountProperty =
        //    DependencyProperty.Register("Count", typeof(int), typeof(MainWindow), new UIPropertyMetadata(12));
    
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            vm.UpdateCount();
        }
    
    
    }
    
    public class ViewModel : INotifyPropertyChanged
    {
    
        public ViewModel()
        {
    
        }
    
        private int dependOne = 0;
    
        public int DependOne
        {
            get
            {
                return dependOne;
            }
            set
            {
                dependOne = value;
                OnPropertyChanged("DependOne");
            }
        }
    
        private int dependTwo = 0;
    
        public int DependTwo
        {
            get
            {
                return dependTwo;
            }
            set
            {
                dependTwo = value;
                OnPropertyChanged("DependTwo");
            }
        }
    
        #region --  INotifyPropertyChanged Members  --
    
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyNameArg)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
    
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyNameArg));
            }
        }
        #endregion
    
        internal void UpdateCount()
        {
            DependOne = 3;
            DependTwo = 4;
        }
    }
    
    public class MyConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var dependOne = (int)values[0];
            var dependTwo = (int)values[1];
    
            return (dependOne + dependTwo).ToString();
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All I need is a way to make a property of one class only
I need to make one function in a module platform-independent by offering several implementations,
I need to make a column unique in one of our database tables, and
I need to make a query to one of Google's services. I read this
I need to make sure that user can run only one instance of my
I need to make so that my application can have only one instance running
I need to have two UITableViews on one UIView. I can make it work
i Have an arguments like the one below which i pass to powershell script
I'm using Python re to try to make a regular expression which finds all
I need to use one of the resourceful controllers plugins - resources_controller/ resource_controller/make_resourceful as

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.