Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all!

I kindly ask help with databinding my own dependency property.

I have usercontrol where I have slider named "valueSlider". I need to change sliders value from client side using custom dependency property.

My usercontrolside code:

public int SliderCurrentValue
        {
            get { return (int)GetValue(SliderCurrentValueProperty); }
            set { SetValue(SliderCurrentValueProperty, value); }
        }

public static readonly DependencyProperty SliderCurrentValueProperty =
            DependencyProperty.Register("SliderCurrentValue", typeof(int), typeof(MySliderControl), new UIPropertyMetadata(0, new PropertyChangedCallback(SliderCurrentValueChanged)));

        private static void SliderCurrentValueChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs args)
        {
            //cast object
            MySliderControl c = (MySliderControl)depObj;

            //sets new value to valueslider
            c.valueSlider.Value = Convert.ToDouble(args.NewValue);
        }




and clientside code where I create databindings between my dataClass and my usercontrol (that slider value)

Binding binding = new Binding("MyIntProperty"); //type of int
binding.Mode = BindingMode.TwoWay;
binding.Source = myDataClass;

//works directly if I allow my usercontrol to show its slidercontrol
MyUserControl.valueSlider.SetBinding(Slider.ValueProperty, binding);  //works, but not wanted to use directly

//This is wanted situation, but doesnt work
MyUserControl.SetBinding(MyUserControl.SliderCurrentValue, binding);



The easiest solution is to show all my valueSlider properties to outside like I show above, but I really need to show just its slidervalue.

I hope you got idea :)

Cheers
Posted

1 solution

You can Binding the valueSlider.Value to MyUserControl.SliderCurrentValue on the UserControl Loaded event. I hope it would work.
 
Share this answer
 
Comments
paleGegg0 29-Dec-10 11:27am    
Im afraid that wont fit my problem. If I got this right I had to do this on usercontrol code side so the binding will be allways to same target. My client wants multiple usercontrols with different datasources.
paleGegg0 31-Dec-10 0:49am    
Now I managed to solve that problem. Your tip helped a lot! As a beginner of C# coder I appreciate your effort :)
Venkatesh Mookkan 31-Dec-10 0:58am    
You can vote or mark it as answer, if it really helps you.

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