c# - Applying MahApps.Metro style to NavigationWindow -
has had luck applying mahapps.metro style navigationwindow? have implemented window fine, need apply navigationwindow pages. tried extending navigationwindow , adding modifications metrowindow so, no luck. window has standard title bar , border, , content black.
using system; using system.windows; using system.windows.input; using system.windows.interop; using system.windows.media; using system.windows.navigation; using mahapps.metro.native; namespace mahapps.metro.controls { [templatepart(name = part_titlebar, type = typeof(uielement))] [templatepart(name = part_windowcommands, type = typeof(windowcommands))] public class metronavigationwindow : navigationwindow { private const string part_titlebar = "part_titlebar"; private const string part_windowcommands = "part_windowcommands"; public static readonly dependencyproperty showiconontitlebarproperty = dependencyproperty.register("showiconontitlebar", typeof(bool), typeof(metronavigationwindow), new propertymetadata(true)); public static readonly dependencyproperty showtitlebarproperty = dependencyproperty.register("showtitlebar", typeof(bool), typeof(metronavigationwindow), new propertymetadata(true)); public static readonly dependencyproperty showminbuttonproperty = dependencyproperty.register("showminbutton", typeof(bool), typeof(metronavigationwindow), new propertymetadata(true)); public static readonly dependencyproperty showclosebuttonproperty = dependencyproperty.register("showclosebutton", typeof(bool), typeof(metronavigationwindow), new propertymetadata(true)); public static readonly dependencyproperty showmaxrestorebuttonproperty = dependencyproperty.register("showmaxrestorebutton", typeof(bool), typeof(metronavigationwindow), new propertymetadata(true)); public static readonly dependencyproperty titlebarheightproperty = dependencyproperty.register("titlebarheight", typeof(int), typeof(metronavigationwindow), new propertymetadata(30)); public static readonly dependencyproperty titlecapsproperty = dependencyproperty.register("titlecaps", typeof(bool), typeof(metronavigationwindow), new propertymetadata(true)); public static readonly dependencyproperty savewindowpositionproperty = dependencyproperty.register("savewindowposition", typeof(bool), typeof(metronavigationwindow), new propertymetadata(false)); public static readonly dependencyproperty windowplacementsettingsproperty = dependencyproperty.register("windowplacementsettings", typeof(iwindowplacementsettings), typeof(metronavigationwindow), new propertymetadata(null)); public static readonly dependencyproperty titleforegroundproperty = dependencyproperty.register("titleforeground", typeof(brush), typeof(metronavigationwindow)); public static readonly dependencyproperty ignoretaskbaronmaximizeproperty = dependencyproperty.register("ignoretaskbaronmaximize", typeof(bool), typeof(metronavigationwindow), new propertymetadata(false)); public static readonly dependencyproperty glowbrushproperty = dependencyproperty.register("glowbrush", typeof(solidcolorbrush), typeof(metronavigationwindow), new propertymetadata(null)); public static readonly dependencyproperty flyoutsproperty = dependencyproperty.register("flyouts", typeof(flyoutscontrol), typeof(metronavigationwindow), new propertymetadata(null)); public static readonly dependencyproperty windowtransitionsenabledproperty = dependencyproperty.register("windowtransitionsenabled", typeof(bool), typeof(metronavigationwindow), new propertymetadata(true)); bool isdragging; public bool windowtransitionsenabled { { return (bool)this.getvalue(windowtransitionsenabledproperty); } set { setvalue(windowtransitionsenabledproperty, value); } } public flyoutscontrol flyouts { { return (flyoutscontrol)getvalue(flyoutsproperty); } set { setvalue(flyoutsproperty, value); } } public windowcommands windowcommands { get; set; } public bool ignoretaskbaronmaximize { { return (bool)this.getvalue(ignoretaskbaronmaximizeproperty); } set { setvalue(ignoretaskbaronmaximizeproperty, value); } } public brush titleforeground { { return (brush)getvalue(titleforegroundproperty); } set { setvalue(titleforegroundproperty, value); } } public bool savewindowposition { { return (bool)getvalue(savewindowpositionproperty); } set { setvalue(savewindowpositionproperty, value); } } public iwindowplacementsettings windowplacementsettings { { return (iwindowplacementsettings)getvalue(windowplacementsettingsproperty); } set { setvalue(windowplacementsettingsproperty, value); } } public bool showiconontitlebar { { return (bool)getvalue(showiconontitlebarproperty); } set { setvalue(showiconontitlebarproperty, value); } } public bool showtitlebar { { return (bool)getvalue(showtitlebarproperty); } set { setvalue(showtitlebarproperty, value); } } public bool showminbutton { { return (bool)getvalue(showminbuttonproperty); } set { setvalue(showminbuttonproperty, value); } } public bool showclosebutton { { return (bool)getvalue(showclosebuttonproperty); } set { setvalue(showclosebuttonproperty, value); } } public int titlebarheight { { return (int)getvalue(titlebarheightproperty); } set { setvalue(titlebarheightproperty, value); } } public bool showmaxrestorebutton { { return (bool)getvalue(showmaxrestorebuttonproperty); } set { setvalue(showmaxrestorebuttonproperty, value); } } public bool titlecaps { { return (bool)getvalue(titlecapsproperty); } set { setvalue(titlecapsproperty, value); } } public solidcolorbrush glowbrush { { return (solidcolorbrush)getvalue(glowbrushproperty); } set { setvalue(glowbrushproperty, value); } } public string windowtitle { { return titlecaps ? title.toupper() : title; } } public metronavigationwindow() { loaded += this.metrowindow_loaded; } private void metrowindow_loaded(object sender, routedeventargs e) { visualstatemanager.gotostate(this, "afterloaded", true); if (!showtitlebar) { //disables system menu reasons other clicking invisible titlebar. intptr handle = new windowinterophelper(this).handle; unsafenativemethods.setwindowlong(handle, unsafenativemethods.gwl_style, unsafenativemethods.getwindowlong(handle, unsafenativemethods.gwl_style) & ~unsafenativemethods.ws_sysmenu); } if (this.flyouts == null) { this.flyouts = new flyoutscontrol(); } } static metronavigationwindow() { defaultstylekeyproperty.overridemetadata(typeof(metronavigationwindow), new frameworkpropertymetadata(typeof(metronavigationwindow))); } public override void onapplytemplate() { base.onapplytemplate(); if (windowcommands == null) windowcommands = new windowcommands(); var titlebar = gettemplatechild(part_titlebar) uielement; if (showtitlebar && titlebar != null) { titlebar.mousedown += titlebarmousedown; titlebar.mouseup += titlebarmouseup; titlebar.mousemove += titlebarmousemove; } else { mousedown += titlebarmousedown; mouseup += titlebarmouseup; mousemove += titlebarmousemove; } } protected override void onstatechanged(eventargs e) { if (windowcommands != null) { windowcommands.refreshmaximiseiconstate(); } base.onstatechanged(e); } protected void titlebarmousedown(object sender, mousebuttoneventargs e) { var mouseposition = e.getposition(this); bool isiconclick = showiconontitlebar && mouseposition.x <= titlebarheight && mouseposition.y <= titlebarheight; if (e.changedbutton == mousebutton.left) { if (isiconclick) { if (e.clickcount == 2) { close(); } else { showsystemmenuphysicalcoordinates(this, pointtoscreen(new point(0, titlebarheight))); } } else { intptr windowhandle = new windowinterophelper(this).handle; unsafenativemethods.releasecapture(); var wpfpoint = pointtoscreen(mouse.getposition(this)); short x = convert.toint16(wpfpoint.x); short y = convert.toint16(wpfpoint.y); int lparam = x | (y << 16); unsafenativemethods.sendmessage(windowhandle, constants.wm_nclbuttondown, constants.ht_caption, lparam); if (e.clickcount == 2 && (resizemode == resizemode.canresizewithgrip || resizemode == resizemode.canresize)) { windowstate = windowstate == windowstate.maximized ? windowstate.normal : windowstate.maximized; } } } else if (e.changedbutton == mousebutton.right) { showsystemmenuphysicalcoordinates(this, pointtoscreen(mouseposition)); } } protected void titlebarmouseup(object sender, mousebuttoneventargs e) { isdragging = false; } private void titlebarmousemove(object sender, mouseeventargs e) { if (e.leftbutton != mousebuttonstate.pressed) { isdragging = false; } if (isdragging && windowstate == windowstate.maximized && resizemode != resizemode.noresize) { // calculating correct left coordinate multi-screen system. point mouseabsolute = pointtoscreen(mouse.getposition(this)); double width = restorebounds.width; double left = mouseabsolute.x - width / 2; // check if mouse @ top of screen if titlebar not visible if (!showtitlebar && mouseabsolute.y > titlebarheight) return; // aligning window's position fit screen. double virtualscreenwidth = systemparameters.virtualscreenwidth; left = left + width > virtualscreenwidth ? virtualscreenwidth - width : left; var mouseposition = e.mousedevice.getposition(this); // when dragging window down @ top of border, // move window bit upwards avoid showing resize handle mouse button released top = mouseposition.y < 5 ? -5 : mouseabsolute.y - mouseposition.y; left = left; // restore window normal state. windowstate = windowstate.normal; } } internal t getpart<t>(string name) t : dependencyobject { return (t)gettemplatechild(name); } private static void showsystemmenuphysicalcoordinates(window window, point physicalscreenlocation) { if (window == null) return; var hwnd = new windowinterophelper(window).handle; if (hwnd == intptr.zero || !unsafenativemethods.iswindow(hwnd)) return; var hmenu = unsafenativemethods.getsystemmenu(hwnd, false); var cmd = unsafenativemethods.trackpopupmenuex(hmenu, constants.tpm_leftbutton | constants.tpm_returncmd, (int)physicalscreenlocation.x, (int)physicalscreenlocation.y, hwnd, intptr.zero); if (0 != cmd) unsafenativemethods.postmessage(hwnd, constants.syscommand, new intptr(cmd), intptr.zero); } } }
to accomplish this, using metrowindow
main navigation window, , frame
within metrowindow
handles navigation.
navigation window
<controls:metrowindow x:class="testwpfapplicationmahapps.metro.navwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="clr-namespace:mahapps.metro.controls;assembly=mahapps.metro" title="navwindow" height="300" width="300"> <window.resources> <resourcedictionary> <resourcedictionary.mergeddictionaries> <resourcedictionary source="pack://application:,,,/mahapps.metro;component/styles/colours.xaml" /> <resourcedictionary source="pack://application:,,,/mahapps.metro;component/styles/fonts.xaml" /> <resourcedictionary source="pack://application:,,,/mahapps.metro;component/styles/controls.xaml" /> <resourcedictionary source="pack://application:,,,/mahapps.metro;component/styles/accents/blue.xaml" /> <resourcedictionary source="pack://application:,,,/mahapps.metro;component/styles/accents/baselight.xaml" /> </resourcedictionary.mergeddictionaries> </resourcedictionary> </window.resources> <frame source="page1.xaml" navigationuivisibility="hidden"></frame> </controls:metrowindow>
this allows me not change of navigation logic, can called navigationservice
when using navigationwindow
.
page 1 .cs
/// <summary> /// interaction logic page1.xaml /// </summary> public partial class page1 : page { public page1() { initializecomponent(); } private void button1_click(object sender, routedeventargs e) { navigationservice.navigate(new page2()); } }
Comments
Post a Comment