繁体   English   中英

silverlight绑定到自定义控件中的依赖项属性

[英]silverlight binding to a dependency property in a custom control

我已经创建了一个自定义控件,它在我的视图中实例化,在我的自定义控件(文本框)中,我有一个依赖属性设置,它绑定到文本值,这一切都正常并更新。 但是我现在想要将我的视图的依赖属性绑定到我的控件的依赖属性,但我不能让它工作得很好。 这是我的意思

控制选定的位

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

查看所选位

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

我想做这个:

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

和视图中的代码

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

那么我需要做些什么才能在我的视图中绑定我的int值Destination,使其与我的自定义控件中的TextValue具有相同的值? 谢谢 :)

ps hi ash

好吧哦,我现在实际上已经对这个问题进行了排序,为了别人的利益,我会试着记下如何在没有我的代码的情况下尽我所能做到最好。

好吧首先我的自定义控件,我没有正确使用依赖属性(哈哈)开始。 所以先让我们纠正。

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

相当多的变化,最好的方法是查看公共FieldValue,这是CustomControl的一部分,它使用GetValue和SetValue作为依赖属性的方法,当你使用SetValue时它使用依赖属性,当值更改依赖项属性调用FieldValueChanged方法,您指定它在DependencyProperty.Register元数据中调用它。 此方法处理要更改的FieldValue,然后在CustomControlViewModel中设置FieldValue,此public int的集合设置我们访问的私有值以返回fieldvalue,但是您还注意到也调用了RaisePropertyChanged(“FieldValue”)方法。 现在这个方法继承自实现此方法的BaseClass,它基本上实现了INotifyPropertyChanged的东西。 所以现在当我的值基本上改变时,事件被推送,现在我需要做的就是在我的视图中处理包含此Control的事件。 这个位是Constuctor中的一个简单位

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

然后一个简单的方法

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

然后使用来自控件的FieldValue更新我的FieldValue视图,那里有一些漏洞,但它是我能记住/解释给你的最好的,希望这有助于人们,我当然明白依赖属性现在好多了:)

当我明天回到电脑上时,我会接受这个答案作为接受的答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM