I'm trying to navigate to an XAML control (specifically ModernFrame control) via the following code when a keyboard shortcut is pressed:
LinkNavigator.Navigate(new Uri("/Controls/SettingsManager.xaml", UriKind.Relative), this);
The keyboard shortcut fires and then I am given an exception:
System.ArgumentException: 'Unable to navigate to /Controls/SettingsManager.xaml, could not find a ModernFrame target '''
The following is the source for the SettingsManager as a ModernFrame--note that this still works if you change it to a UserControl. I changed it to a ModernFrame because of the aforementioned exception which is looking for a ModernFrame.
Now the SettingsManager.xaml control functions perfectly fine if I navigate to it via a TitleLink within the window. However the moment I try to navigate to it programmatically I receive the exception. You can leave the control completely empty and the exception is still thrown.
SettingsManager.xaml.cs:
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using FirstFloor.ModernUI.Presentation;
using FirstFloor.ModernUI.Windows.Controls;
using KeystoneEstimating.Containers;
namespace KeystoneEstimating.Controls {
/// <summary>
/// Interaction logic for SettingsManager.xaml
/// </summary>
public partial class SettingsManager : ModernFrame {
public SettingsManager() {
InitializeComponent();
/// Load settings into the Link interface of MUI.
List<AppSettings> settings = AppInfo.SettingsContainers;
foreach (AppSettings set in settings) {
Link lnk = new Link();
lnk.DisplayName = set.SettingsName;
lnk.Source = set.ControlPath;
SettingsLinks.Links.Add(lnk);
}
// Load up the very first registered settings page.
if (SettingsLinks.Links.First() != null)
SettingsLinks.SelectedSource = SettingsLinks.Links.First().Source;
}
}
}
SettingsManage.xaml:
<mui:ModernFrame xmlns:mui="http://firstfloorsoftware.com/ModernUI"
x:Class="KeystoneEstimating.Controls.SettingsManager"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:KeystoneEstimating"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Style="{StaticResource ContentRoot}">
<mui:ModernTab Layout="List" Name="SettingsLinks"/>
</Grid>
</mui:ModernFrame>
ModernUI comes in two NugetPackages labeled ModernUI.WPFCore. One is version 2.0.0 and the other is version 3.1.2. The problem is reproducible on both.
Seems like there is an inherent issue with FirstFloor ModernUI's Navigation command where it cannot find the MainWindow.xaml's
ContentFramewhich is a style control of typeModernFramewhen callingNavigate().I searched into the style from the source code and found that through the XAML the
ContentFrameis being found via WPF bindings.When you click any
Linkin aModernWindowthis is how the code is handled and works perfectly when there you've precoded the UI elements in XAML because all XAML elements will automatically inherit theModernWindowas their parent object. Which is necessary for the Navigate command as it does a search up the element hierarchy of the parent to find a ModernFrame to populate into. SeeDefaultLinkNavigator.Navigatecalls a functionNavigationHelper.FindFrame(parameter, source):However this is not the case in code... When executing links in code the XAML page of the control you wish to navigate to doesn't exist yet and as such has no parent and cannot be used to find the parent's
ModernFrameon navigation.The solution however is to either to create a binding in code and pass the binding as a parameter to the Navigate command--which I don't know how to do yet--or do a hierarchical search down the
ModernWindowcontrol and manually search for all source elements of typeModernFramewith the nameContentFrame(which is the display element of the window in theModernWindow.xamlthat shows your content.First add a hierarchical search function to your
MainWindow.csclass--which should be extendingModernWindowinstead ofWindowin WPF:Within the same class to navigate to a control via code--in my case when a keybinding is executed--you can use three different methods:
Source for hierarchy child search: https://stackoverflow.com/a/1759923/2808956