Hi Vijayalakshmi VR,
That is an instructive example! Thank you so much.
I prefer an approach that has even less code behind the xaml files than your last example and it seems I have found a way. I will describe my approach for future reference. Should you need a fully working code example, let me know and I will be happy to create it.
- I created a custom usercontrol that consists of a textbox + button. This usercontrol is specific to the new window that I want to interact with. Due to limitations of C# I cannot create a generic version of this usercontrol (or at least, it would not be straightforward). As far as I understand it, a code behind file cannot be initialized with a generic parameter T.
- The custom usercontrol has code-behind its xaml file. It handles the Button_Click to open the custom window (using caliburn micro). I also created a DependencyProperty (SelectedContentProperty), like in your example and called the associated property SelectedContent. The SelectedContent property is of a custom datatype class. The Xaml TextBox.Text parameter is bound to the SelectedContent.Description property (type string).
- When pressing the OK button on the custom window, a custom event is advertised on the caliburn micro event aggregator.
- The CustomEditor for my propertygrid custom usercontrol is subscribed to this event aggregator and handles the event type that was published by my custom window.
- In the CustomEditor, I have access to the usercontrol SelectedContent property and the advertised event contains a copy of the data model I selected in the custom window. In the CustomEditor event handler for my window event, I set the SelectedContent property of my custom usercontrol.
Since this setup visually produced the result I wanted, I initially thought my search was over. Alas, the custom usercontrol was only displaying a value, but it was not yet attached to the designated property in the propertygrid. For this to work, I looked at this example: https://www.syncfusion.com/forums/138092/how-to-detect-change-on-property-of-selectedobject
- I added a TextChanged event to the code behind file of my custom usercontrol:
public event TextChangedEventHandler TextChanged;
private void Text_TextChanged(object sender, TextChangedEventArgs e)
{
this.TextChanged?.Invoke(this, e);
}
- In the viemodel of the view that contains the propertygrid, I added an eventhandler to the _PropertyGrid_SelectedPropertyItemChanged event following the pattern that was outlined in the forum post that I cited. I implemented the example in the ViewModel associated with my View, and not in the code behind file of my View, as shown in the example. Only then I was able to set the designated target of my custom Window with the desired value.
This approach follows a more clean MVVM pattern since there is only code behind the custom usercontrol xaml file. For the rest the View and Viewmodel containing the propertygrid from which my custom window was launched, were fully separated.
I feel that it could be valuable to include the info in this example and the referenced example at https://www.syncfusion.com/forums/138092/how-to-detect-change-on-property-of-selectedobject to the propertygrid manual.
Thank you again for your help!
Niels van Strien