Getting and Setting Attached Property Values in Code

Although it's a somewhat different topic to data binding, getting and setting the value of an attached property on a control is similar to getting and setting bindings. Use the GetValue method of a control, passing it the dependency property corresponding to the attached property, to get the given attached property's value for that control. For example, use the following code to get which grid row a text box control named NameTextBox is in (hence, get the value of the Grid.Row attached property on the text box):

int row = (int)NameTextBox.GetValue(Grid.RowProperty);

You can then assign a different value to that attached property if you wish, using the SetValue method of the control, and passing it the corresponding dependency property and the new value to assign to it:

NameTextBox.SetValue(Grid.RowProperty, row);

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset