Binding silverlight to a dependency property in a custom control

I have my own custom control created in my view, in my custom control (text field) I have set the dependency property, which is bound to the text value, everything works correctly and is updated. However, now I want to bind the dependency property of my view to the dependency property of my control, but I cannot get it to work. Here is what i mean

Control selected bits

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

View selected bits

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

I want to do this:

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

and code of the form

 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 int , , TextValue, ? :)

ps hi ash

+3
1

, , , , , .

, , , Dependency (-) . .

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

, , public FieldValue, CustomControl, GetValue SetValue , , SetValue, , dependency FieldValueChanged, DependencyProperty.Register. , FieldValue, FieldValue CustomControlViewModel, int, , , , , , RaisePropertyChanged ( "FieldValue" ). BaseClass, , INotifyPropertyChanged. , , , , , , , . Constuctor

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

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

FieldValue FieldValue , , , / , , , , , Dependency Properties :)

,

+1

Source: https://habr.com/ru/post/1757808/


All Articles