Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ideally you do not want to use view logic in your viewmodel. Your viewmodel should not know anything about the view. It would be a better idea for your viewmodel to set a property letting the view know it's time to navigate. Here's an example:</p> <p><strong>ViewModel</strong>:</p> <pre><code>using System.ComponentModel; namespace ViewModels { public class MyViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(string propertyName) { if (PropertyChanged == null) return; PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } private bool _DoneDoingStuff; public bool DoneDoingStuff { get { return _DoneDoingStuff; } set { if (_DoneDoingStuff != value) { _DoneDoingStuff = value; NotifyPropertyChanged("DoneDoingStuff"); } } } } } </code></pre> <p><strong>View</strong>:</p> <pre><code>&lt;navigation:Page x:Class="Views.MyView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" xmlns:vm="clr-namespace:ViewModels"&gt; &lt;navigation:Page.Resources&gt; &lt;vm:MyViewModel x:Key="MyViewModelInstance" /&gt; &lt;/navigation:Page.Resources&gt; &lt;Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource MyViewModelInstance}}"&gt; &lt;i:Interaction.Triggers&gt; &lt;ei:DataTrigger Binding="{Binding DoneDoingStuff}" Value="True"&gt; &lt;ei:HyperlinkAction NavigateUri="AnotherPage.xaml" /&gt; &lt;/ei:DataTrigger&gt; &lt;/i:Interaction.Triggers&gt; &lt;/Grid&gt; &lt;/navigation:Page&gt; </code></pre> <ul> <li><p>Use a <code>DataTrigger</code> with <code>Binding</code> property set to the <code>DoneDoingStuff</code> property from your viewmodel, and the <code>Value</code> property set to "True". The <code>DataTrigger</code> will trigger when <code>DoneDoingStuff</code> from your viewmodel is set to true.</p></li> <li><p>Now you need a trigger action to navigate. Use <code>HyperlinkAction</code> with the <code>NavigateUri</code> property set to the page you're navigating to.</p></li> <li><p>Be sure to have <strong>System.Windows.Interactivity</strong>, <strong>System.Windows.Controls.Navigation</strong>, and <strong>Microsoft.Expression.Interactions</strong> assemblies in your references.</p></li> </ul> <p>At first, this might seem to be too much, but your view logic is now where it needs to be.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload