views:

279

answers:

1

I've got a custom control ive created that is instanced in my view, in my custom control (a text box) i have a dependency property set up which is bound to the text value, this all works correctly and updates. However I now want to bind a dependency property of my view to the dependency property of my control but I cant get this to quite work. Heres 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 from view

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

So what do i need to do to be able to bind my int value Destination in my view to have the same value as my TextValue from my custom control? Thanks :)

ps hi ash

A: 

Righty oh, I have actually now sorted this problem and for the benefit of others I'll try and note down how I did the best I can without my code in front of me.

Okay so first of all my custom control, I wasnt utilising Dependency properties properly (haha) to begin with. So lets rectify that first.

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

Quite a few changes there, best way to think of it is to look at the public FieldValue thats part of CustomControl which uses GetValue and SetValue as simply as methods for your dependency property, when you use SetValue it uses the dependency property, when the value changes the dependency property calls the FieldValueChanged method, which you specified it to call in the DependencyProperty.Register metadata. This method that handles the FieldValue being changed then sets the FieldValue in the CustomControlViewModel the set for this public int sets the private value that we access to return the fieldvalue but as you notice also calls the RaisePropertyChanged("FieldValue") method. Now this method is inherited from my BaseClass which implements this method, it basically implements the INotifyPropertyChanged stuff. So now when my value changes basically an event is pushed and now all i need to do is handle that event in my view which contains this Control. This bit is a simple bit in the Constuctor

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

Then a method for that simply

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

That then updates my FieldValue of the view with the FieldValue from the control, thats got some holes in it but its the best I can remember/explain it to you, hope this helps people, I certainly understand Dependency Properties a lot better now :)

I'll accept this answer as the accepted answer when I'm back on computer tomorrow

Lee
This method works but is not desirable I will update with the correct way of doing this with dependency properties when I get time
lee