Is it possible to load user controls on demand? - Cont.

jennyliu835 221 Reputation points
2020-10-01T06:47:35.623+00:00

Hi Peter + Daisy,

Is it possible to load a specified user control (i.e. UC1, UC2,... UC# ) without building any list, just as we do with a specified page? For example,
NavigationService NS = fm.NavigationService;
NS.Navigate(new Uri("pages/Page2.xaml", UriKind.Relative));

Thanks

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,683 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-10-01T09:01:45.033+00:00

    Hi Jenny,
    yes it is possible. You can load UserControl from file and display it in ContentControl. Try following demo. Window87UC1.xaml and Window87UC2.xaml are xaml files (UserControl without CodeBehind). This xaml files are included in project as Content.

    Window87UC1.xaml

    <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"   
                 mc:Ignorable="d"   
                 d:DesignHeight="450" d:DesignWidth="800">  
      <Grid>  
        <Label Content="I am UserControl 1" Background="LightGreen"/>  
      </Grid>  
    </UserControl>  
    

    Window87UC2.xaml

    <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"   
                 mc:Ignorable="d"   
                 d:DesignHeight="450" d:DesignWidth="800">  
      <Grid>  
        <Label Content="I am UserControl 2" Background="LightPink"/>  
      </Grid>  
    </UserControl>  
    

    XAML of MainWindow

    <Window x:Class="WpfApp1.Window86"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp86"  
            mc:Ignorable="d"  
            Title="MainWindow" Height="450" Width="800">  
      <Window.DataContext>  
        <local:ViewModel/>  
      </Window.DataContext>  
      <StackPanel>  
        <Button Content="Display UC 1" Command="{Binding}" CommandParameter="UC1" Margin="5"/>  
        <Button Content="Display UC 2" Command="{Binding}" CommandParameter="UC2" Margin="5"/>  
        <ContentControl Grid.Row="1" Content="{Binding DisplayUC}" Height="80"/>  
      </StackPanel>  
    </Window>  
    

    ViewModel:

    using System;  
    using System.ComponentModel;  
    using System.IO;  
    using System.Runtime.CompilerServices;  
    using System.Windows;  
    using System.Windows.Controls;  
    using System.Windows.Input;  
    using System.Windows.Markup;  
      
    namespace WpfApp86  
    {  
      public class ViewModel : ICommand, INotifyPropertyChanged  
      {  
        public UserControl DisplayUC { get; set; }  
        public void Execute(object parameter)  
        {  
          DisplayUC = (UserControl)XamlReader.Load(new FileStream($"Window87{parameter}.xaml", FileMode.Open));  
          OnPropertyChanged(nameof(DisplayUC));  
        }  
      
        public event EventHandler CanExecuteChanged;  
        public bool CanExecute(object parameter) => true;  
        public event PropertyChangedEventHandler PropertyChanged;  
        private void OnPropertyChanged([CallerMemberName] string propName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));  
      }  
    }  
    

    Result:

    29527-x.gif

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful