2

I have MainWindow.xaml that has ContentControl. I have 4 UserControls that i created. I want to change content of ContentControl in MainWindow.xaml when button pressed inside of my UserControl. Here is my MainWindow.xaml:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:KIOSK" mc:Ignorable="d" x:Class="KIOSK.MainWindow"
        Title="MainWindow" Height="350" Width="525" WindowState="Maximized">
    <ContentControl Name="contentMain">
        <local:main_screen />
    </ContentControl>
</Window>

Here my UserControls:

1)main_screen.xaml

<UserControl x:Class="KIOSK.main_screen"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid Name="grid1" ShowGridLines="True">            
        <Button Margin="10" Background="#FFA4F200" Click="Button_Click"/>                        
    </Grid>
</UserControl>

2) ClubRules.xaml

<UserControl x:Class="KIOSK.ClubRules"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Background="White">
    <Grid ShowGridLines="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="grid1">            

        <Button Margin="0,15,0,15" Background="#FFFE0555" HorizontalAlignment="Center" Click="Button_Click" />                           
    </Grid>
</UserControl>

Inside of main_creen.xaml.cs i wrote for button pressed:

ClubRules cr = new ClubRules();
MainWindow mw = new MainWindow();
mw.contentMain.Content = new ClubRules();

But its not working.. I want to change Content of ContentControl inside of UserControl when button pressed.

9
  • Is the instance of the MainWindow that you have created, being displayed the user? Commented Apr 13, 2015 at 4:22
  • @bit, I don't understand you properly. But user see MainWindow.xaml and i want to change (Navigate him) Content of ContentControl. I change UserControls. Commented Apr 13, 2015 at 4:29
  • 1
    You seem to have used the newly created instance of the MainWindow(), instead try contentMain.Content = new ClubRules(); directly.. Commented Apr 13, 2015 at 4:30
  • @bit, As you see, button inside of UserControl. Could you provide me some example? Commented Apr 13, 2015 at 4:33
  • The problem is that there is an instance of a MainWindow displayed and another instance that you created on the button press event. And you are assigning the ClubRules to the later one. You will have to figure out a way to get the reference of the diaplyed MainWindow and assign the ClubRules to it Commented Apr 13, 2015 at 4:37

2 Answers 2

3

Use delegates and events for your scenario. Publish an event main_screen.xaml.cs and Subscribe the event in MainWindow.xaml.cs

Publish the event

main_screen.xaml.cs

public partial class main_screen: UserControl
{
    public Delegate del;
    public main_screen()
    {
        InitializeComponent();
    }
    public void method1()
    {
        del.DynamicInvoke();
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        method1();
    }
}

Subscribe that event in MainWindow.xaml.cs

MainWindow.Xaml

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:KIOSK" mc:Ignorable="d" x:Class="KIOSK.MainWindow"
        Title="MainWindow" Height="350" Width="525" WindowState="Maximized">
    <ContentControl Name="contentMain">
        <local:main_screen x:Name="main_screen_obj" />
    </ContentControl>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public delegate void ValuePassDelegate();
    public event ValuePassDelegate ValuePassEvent;

    public MainWindow()
    {
        InitializeComponent();
        ValuePassEvent += new ValuePassDelegate(method1);
        main_screen_obj.del = ValuePassEvent;
    }
    public void method1()
    {
        contentMain.Content = new ClubRules();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You are creating a new MainWindow() instead of using the one that is being displayed. You should be assigning the ClubRules to the Content of the one that is being displayed.

One way to do that is by bringing the button from the UserControl to the MainWindow itself. Other way as suggested by @decoherence is by using the singleton pattern.

There could be more ways, but you basically need to use the same instance of the MainWindow that has been displayed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.