Pregunta

I am new on stackoverflow and relatively new to WPF.

I've wrapped my head around a half dozen weighty tomes of Patterns and Best Practices (as well as numerous posts here) but cannot seem to find the solution I am looking for.

My Problem: WPF / .Net 4 / C# I have a text processor (of type Editor E) that can load one Document (of type Document D) at a time (strored as Editor.CurrentDocument). Several UI controls bind to the Document's properties (all Dependency Properties) such as Document.Title, Document.DateLastModification.

Now I want to be able to switch the actual Document instance without having to unhook and re-hook all event handlers. So I guess the Editor.CurrentDocument property must somehow remain its instance while switching its implementation.

I have tried to create a SingleInstanceDocument class that inherits directly from Document and uses the Singleton pattern. But then I cannot find a way to inject any Document instance into the SingleInstanceDocument without having to internally re-map all properties.

Am I somehow being misguided or missing the point here? If the SingleInstanceDocument approach is a viable solution, is there any way I can use reflection to re-map all available dependency properties from the inner Document to the outer SingleInstanceDocument shell automatically?

Thank you very much!

Addendum:

It turned out that the functionality required here was already provided by WPF/.NET out of the box by implementing INotifyPropertyChanged on the CurrentDocument host object. Thus changing the current document caused the UI to update its bound controls appropriately. I'm sorry for all the confusion.

¿Fue útil?

Solución

first, learn some basic MVVM pattern. basically in WPF-MVVM just use ObservableCollection and INotifyPropertyChanged interface.

this type of collection implements the observer pattern that notify update to UI(View) when you add/remove or "select" the current item.

//in main ViewModel
private Ducument _currentDocument;

public Document CurrentDocument 
{ 
    get { return _currentDocument; }
    set
    {
        _currentDocument = value;
        NotifyPropertyChanged("CurrentDocument");
    }
}

//stored all loaded documents as collection.
public ObservableCollection<Document> Documents { get; set; } 

binding selected - current item.

<ListBox ItemsSource="{Binding Path=Documents}" SelectedItem="{Binding Path=CurrentDocument}" DisplayMemberPath="Title">
    <!-- //all Document.Title as listitem -->
</ListBox>
<!--// Editor's View -->
<ContentControl DataContext="{Binding Path=CurrentDocument}"></ContentControl>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top