Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 variable 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???
have a look at below code

XAML:
XML
<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}" />
        <Button Content="Button" Name="button1" Click="button1_Click" />
    </Grid>
</Window>



CODE BEHIND:

C#
using System;
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
        }


    }
}
Posted
Updated 15-Jul-11 22:23pm
v6

1 solution

Hi Pritesh, the solution to your problem is by using PropertyChangedCallback and attach an EventHandler to it.

Here is how I did:

C#
public partial class Window1 : Window
{
    public static DependencyProperty CountProperty;
    public static DependencyProperty DependOneProperty;
    public static DependencyProperty DependTwoProperty;
    static Window1()
    {
        CountProperty = DependencyProperty.Register("Count",
            typeof(int),
            typeof(Window1),
            new UIPropertyMetadata(0));

        // Use PropertyChangedCallBack, whenever DependOne property changes, the handler attached to it will be called.
        DependOneProperty = DependencyProperty.Register("DependOne",
            typeof(int),
            typeof(Window1),
            new UIPropertyMetadata(0,
                new PropertyChangedCallback(OnDependChange)));

        // Same goes for DependTwo but attaching to the same handler.
        DependTwoProperty = DependencyProperty.Register("DependTwo",
            typeof(int),
            typeof(Window1),
            new UIPropertyMetadata(0,
                new PropertyChangedCallback(OnDependChange)));
    }

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

    public int DependOne
    {
        get { return (int)GetValue(DependOneProperty); }
        set { SetValue(DependOneProperty, value); }
    }

    public int DependTwo
    {
        get { return (int)GetValue(DependTwoProperty); }
        set { SetValue(DependTwoProperty, value); }
    }

    private static void OnDependChange(DependencyObject sender,
        DependencyPropertyChangedEventArgs e)
    {
        Window1 window = (Window1)sender;
        if (e.Property == DependOneProperty)
        {
            window.DependOne = (int)e.NewValue;
        }
        else
        {
            window.DependTwo = (int)e.NewValue;
        }
        window.Count = window.DependOne + window.DependTwo;
    }

    public Window1()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        // When you "set" the DependOne and DependTwo, OnDependChange will be called and the Count will be updated.
        DependOne = DependOne + 1;
        DependTwo = DependTwo + 1;
    }
}


Hope this helps. :)

Do let me know if you have any doubts.
 
Share this answer
 
Comments
Pritesh Aryan 16-Jul-11 9:54am    
Thank you so much Tarun..this is the exact answer...
Tarun.K.S 16-Jul-11 10:02am    
Thanks man! Very glad that it helped. :)
Espen Harlinn 17-Jul-11 7:38am    
Very good effort, my 5+
Tarun.K.S 17-Jul-11 7:41am    
Thank you very much! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900