public static void ExportViewToXaml( ViewBase view, string fileName ) { if( view == null ) throw new ArgumentNullException( "view" ); object value; DependencyProperty dp; ResourceDictionary resourceDictionary = new ResourceDictionary(); Type viewType = view.GetType(); Style viewStyle = new Style( viewType ); foreach( FieldInfo fieldInfo in viewType.GetFields( BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static ) ) { dp = fieldInfo.GetValue( view ) as DependencyProperty; if( dp != null ) { value = view.ReadLocalValue( dp ); if( value != DependencyProperty.UnsetValue ) viewStyle.Setters.Add( new Setter( dp, value ) ); } } resourceDictionary.Add( viewType, viewStyle ); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.OmitXmlDeclaration = true; using( XmlWriter xmlWriter = XmlWriter.Create( fileName, settings ) ) { XamlWriter.Save( resourceDictionary, xmlWriter ); } }
private static void ClearViewPropertyValues( ViewBase view ) { if( view == null ) throw new ArgumentNullException( "view" ); DependencyProperty dp; foreach( FieldInfo fieldInfo in view.GetType().GetFields( BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static ) ) { dp = fieldInfo.GetValue( view ) as DependencyProperty; if( dp != null ) view.ClearValue( dp ); } }
public static void ImportViewFromResourceDictionary( ViewBase view, ResourceDictionary resourceDictionary ) { if( view == null ) throw new ArgumentNullException( "view" ); if( resourceDictionary == null ) throw new ArgumentNullException( "resourceDictionary" ); Type viewType = view.GetType(); // Try extracting a implicit style for the specified view from the ResourceDictionary. Style viewStyle = resourceDictionary[ viewType ] as Style; if( viewStyle == null ) { // There is no implicit style for the specified view in the ResourceDictionary. // Use the first Style having the view type as TargetType. Style tempStyle; foreach( object value in resourceDictionary.Values ) { tempStyle = value as Style; if( ( tempStyle != null ) && ( tempStyle.TargetType == viewType ) ) { viewStyle = tempStyle; break; } } } ViewImportExportManager.ImportViewFromStyle( view, viewStyle ); }