WPF数据绑定依赖项属性 [英] WPF databinding dependency property

查看:62
本文介绍了WPF数据绑定依赖项属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我恳请数据绑定我自己的依赖项属性方面的帮助.

我有用户控件,其中有名为"valueSlider"的滑块.我需要使用自定义依赖项属性从客户端更改滑块值.

我的用户控制端代码:

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);
        }





以及在我的dataClass和usercontrol(滑块值)之间创建数据绑定的客户端代码.





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);




最简单的解决方案是像上面显示的那样将我的所有valueSlider属性显示在外部,但是我真的只需要显示其slidervalue.

我希望你有主意:)

干杯




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

推荐答案

您可以在UserControl Loaded事件上将valueSlider.Value绑定到MyUserControl.SliderCurrentValue.我希望它能起作用.
You can Binding the valueSlider.Value to MyUserControl.SliderCurrentValue on the UserControl Loaded event. I hope it would work.


这篇关于WPF数据绑定依赖项属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆