Click here to Skip to main content
15,892,965 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: MouseUp-Event doesn't react on left mouse button Pin
MyPiano7-Aug-09 12:14
MyPiano7-Aug-09 12:14 
GeneralRe: MouseUp-Event doesn't react on left mouse button Pin
Mark Salsbery7-Aug-09 13:13
Mark Salsbery7-Aug-09 13:13 
GeneralRe: MouseUp-Event doesn't react on left mouse button Pin
MyPiano8-Aug-09 8:02
MyPiano8-Aug-09 8:02 
QuestionDatabinding in Silverlight with databases other than with SQL Server Pin
pisanis7-Aug-09 4:53
pisanis7-Aug-09 4:53 
AnswerRe: Databinding in Silverlight with databases other than with SQL Server [modified] Pin
Mike Marynowski7-Aug-09 6:30
professionalMike Marynowski7-Aug-09 6:30 
AnswerRe: Databinding in Silverlight with databases other than with SQL Server [modified] Pin
Mark Salsbery7-Aug-09 7:23
Mark Salsbery7-Aug-09 7:23 
AnswerRe: Databinding in Silverlight with databases other than with SQL Server Pin
Michael Sync8-Aug-09 6:08
Michael Sync8-Aug-09 6:08 
GeneralDynamically loading Silverlight UserControl from a single.xap [modified] Pin
Amit Rai Sharma6-Aug-09 23:37
Amit Rai Sharma6-Aug-09 23:37 
I recently started learning Silverlight. While doing a sample code I got stuck with how to load Silverlight UserControl dynamically from the same .xap file. Imagine a scenario where we have two UserControl; Login and Reports. User would need to login to see the report page. On click of the Login button the Reports UserControl should be loaded and Login UserControl should be unloaded.

I was able to achieve this functionality by using delegate. I will try to explain the bit I have done to achieve this.

Declare a base class which inherits from the UserControl class. The base class will exposes a delegate to handle the dynamic loading of the usercontrol.

BaseUserControl.cs
namespace MyFirstSLApp
{
    public delegate void ControlChangeHandler(object control, int ctrlId);

    public class BaseUserControl : UserControl
    {
        public ControlChangeHandler OnContentChange;
    }
}


Create a Login UserControland inherit it from the BaseUserControl class. Here we will implement the code to notify the subscriber for the UserControl change.


Login.xaml
<control:BaseUserControl xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input"  x:Class="MyFirstSLApp.Login"
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlnsBig Grin="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:control="clr-namespace:MyFirstSLApp"
    Width="400" Height="200" >
    <Grid x:Name="LayoutRoot" Background="White" Width="400">
        <Grid x:Name="LoginLayout" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="70"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions> 
            <Grid.RowDefinitions>
                <RowDefinition Height="20"></RowDefinition>
                <RowDefinition Height="20"></RowDefinition>
                <RowDefinition Height="20"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <TextBlock Text="User Id:" Grid.Column="0" Grid.Row="0" ></TextBlock>
            <TextBox x:Name="txtUserId" Grid.Column="1" Grid.Row="0" ></TextBox>
            <TextBlock Text="Password:" Grid.Column="0" Grid.Row="1"></TextBlock>
            <PasswordBox x:Name="txtPassword" Grid.Column="1" Grid.Row="1"></PasswordBox>
            <Button x:Name="btnLogin" Content="Login" Click="btnLogin_Click" Grid.Column="0" Grid.Row="2"></Button>
            <!--<Button x:Name="btn" Content="Login" Grid.Column="3" Grid.Row="0"
                    Grid.ColumnSpan="2" Grid.RowSpan="2"></Button>-->
            <TextBlock x:Name="txtError" Text="" Grid.Column="0" Grid.Row="4" Grid.ColumnSpan="4"></TextBlock>
        </Grid>

        <dataInput:ValidationSummary >
        </dataInput:ValidationSummary>
    </Grid>
    
</control:BaseUserControl>


Login.xaml.cs
//Notify the subscriber for UserControl change.
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
    if (OnContentChange != null)
            OnContentChange(this, 1);
}


Note: Since we have inherited the Login control from BaseUserControl control, I have used control:BaseUserControl tag instead of UserControl tag. To enable the compiler to recognise BaseUserControl tag, add following entries to AssemblyInfo.cs file

[assembly: XmlnsDefinition("http://schemas.microsoft.com/client/2007", "MyFirstSLApp")]


Create a UserControl which will acts as the container for loading the controls dynamically.

Page.xaml
<UserControl x:Class="MyFirstSLApp.Page"
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:ctrl="clr-namespace:MyFirstSLApp"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
    </Grid>
</UserControl>


Note:Here the <Grid> control will act as a container. We can dynamically add the UserControls to its UIElementCollection. This gives us a flexibility to add or remove a control at runtime.

Finally subscribe the delegate in APP class. Here we will manipluate the Children collection of the Page Control to dynamically display UserControls.

App.xaml.cs
private void Application_Startup(object sender, StartupEventArgs e)
{
    //create instance of Container control
    Page page = new Page();
    //Create instance of Login control
    Login login = new Login();
    //Subscribe to the delegate;
    ContentChange(login);
    //Add the Login control to the <Grid>
    page.LayoutRoot.Children.Add(login);
    //Set the container control as main application UI
    this.RootVisual = page;
}

public void ContentChange(BaseUserControl control)
{
    control.OnContentChange += new ControlChangeHandler(control_OnContentChange);
}

void control_OnContentChange(object control, int ctrlId)
{
    Page page = null;
    switch (ctrlId)
    { 
        case 1:
            //Retrieve the container control
            page = (Page)this.RootVisual;
            //Clear all the element from Children collection of the <Grid>
            page.LayoutRoot.Children.Clear();
            //Create instance of Report control and add it to the children collection of <Grid>
            Reports report = new Reports();
            page.LayoutRoot.Children.Add(report);
            //Subscribe to the delegate
            ContentChange(report);
            break;
        case 2:
            page = (Page)this.RootVisual;
            page.LayoutRoot.Children.Clear();
            Login login = new Login();
            page.LayoutRoot.Children.Add(login);
            ContentChange(login);
            break;
    }
}


AR

modified on Friday, August 7, 2009 7:21 AM

QuestionNeed just idea or suggestion can u help me? Pin
wasimsharp6-Aug-09 23:12
wasimsharp6-Aug-09 23:12 
AnswerRe: Need just idea or suggestion can u help me? Pin
#realJSOP7-Aug-09 0:54
mve#realJSOP7-Aug-09 0:54 
QuestionNeed an advice about starting a raster drawing program with WPF Pin
Cristoff6-Aug-09 22:54
Cristoff6-Aug-09 22:54 
AnswerRe: Need an advice about starting a raster drawing program with WPF Pin
#realJSOP7-Aug-09 0:57
mve#realJSOP7-Aug-09 0:57 
GeneralRe: Need an advice about starting a raster drawing program with WPF Pin
Cristoff7-Aug-09 2:23
Cristoff7-Aug-09 2:23 
GeneralRe: Need an advice about starting a raster drawing program with WPF PinPopular
#realJSOP7-Aug-09 3:43
mve#realJSOP7-Aug-09 3:43 
GeneralRe: Need an advice about starting a raster drawing program with WPF Pin
Cristoff7-Aug-09 3:53
Cristoff7-Aug-09 3:53 
GeneralRe: Need an advice about starting a raster drawing program with WPF Pin
Pete O'Hanlon7-Aug-09 3:56
mvePete O'Hanlon7-Aug-09 3:56 
GeneralRe: Need an advice about starting a raster drawing program with WPF Pin
Cristoff7-Aug-09 4:58
Cristoff7-Aug-09 4:58 
GeneralRe: Need an advice about starting a raster drawing program with WPF Pin
Veldrain7-Aug-09 11:31
Veldrain7-Aug-09 11:31 
GeneralRe: Need an advice about starting a raster drawing program with WPF PinPopular
#realJSOP7-Aug-09 4:25
mve#realJSOP7-Aug-09 4:25 
GeneralRe: Need an advice about starting a raster drawing program with WPF Pin
Cristoff7-Aug-09 4:54
Cristoff7-Aug-09 4:54 
GeneralRe: Need an advice about starting a raster drawing program with WPF Pin
#realJSOP7-Aug-09 4:56
mve#realJSOP7-Aug-09 4:56 
GeneralRe: Need an advice about starting a raster drawing program with WPF Pin
Cristoff7-Aug-09 5:00
Cristoff7-Aug-09 5:00 
GeneralRe: Need an advice about starting a raster drawing program with WPF Pin
Todd Smith7-Aug-09 6:55
Todd Smith7-Aug-09 6:55 
GeneralRe: Need an advice about starting a raster drawing program with WPF Pin
Cristoff7-Aug-09 7:10
Cristoff7-Aug-09 7:10 
AnswerRe: Need an advice about starting a raster drawing program with WPF Pin
Daniel Grunwald7-Aug-09 4:18
Daniel Grunwald7-Aug-09 4:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.