Posts

Showing posts with the label Silverlight

Silverlight: Simple way to create call back method.

The Simple way to create a call back method in .Net. also you can pass on the value to the callback object inside the another call backmethod to your callbackmethod. Private Guid userId; public void SaveAndComplete(bool complete, Action onComplete) { userId= //One more callback method. _dc.SubmitChanges(SaveCompleted, onComplete); } private void SaveCompleted(SubmitOperation so) { if (!so.HasError) { //this is where you can pass on the value to the call back method. ((Action)so.UserState)(UserId); } else { //Error message } } Calling the callback method "SaveAndComplete" private void Save_Click(object sender, RoutedEventArgs e) { SaveAndComplete(false, SavedSuccessFully); } private void SavedSuccessFully(Guid generatedID) { //here you will get the value which you hve passed from the callback method. if (generatedAssessmentID == Guid.Empty) return; }

Silverlight: Finding Parent control

The problem the UserControl's parent is not the ChildWindow, its the Grid inside the child window. You need to get the parent of the parent of the UserControl to navigate to the ChildWindow:- ChildWindow cw = (ChildWindow)((FrameworkElement)this.Parent).Parent; However embedding this in your UserControl would bad practice, you would be stipulating to the consumer of your UserControl where the it can be sited. In the above case for the user control to work it would need to always be a direct child of the Layout root. A better approach would be to search up the visual tree looing for a ChildWindow. I would use this helper method (actually I'd place this in a helper extensions static class but I'll keep it simple here). private IEnumerable Ancestors() { DependencyObject current = VisualTreeHelper.GetParent(this); while (current != null) { yield return current; current = VisualTreeHelper.GetParent(current); } } Now you can use LINQ methods to get the...

Silverlight: Normal Properties vs. Dependency Properties

In the Silverlight development world there are two kinds of properties that you can have on a Silverlight control: normal C# properties and dependency properties. The normal C# properties are added to a Silverlight control the same way as they would be added to any C# class: public string MyProperty { get; set; } And then in XAML, you could set the value of the property like this: <MyControl MyProperty= "someValue" /> This works fine for setting specific values in templates that are unlikely to change or if they do change then the change is always manually made to the XAML file. But what happens if you want to have the value of the property bound to some other value that is retrieved from a database, web service, etc? You might try Binding to the property like so: <MyControl MyProperty= "{Binding DynamicPropertyValue}" /> Where DynamicPropertyValue was a property on the parent element that you wanted to to have your pro...