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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Шыназ Алиш
  • 401
  • 2
  • 7
  • 23
  • Is the instance of the MainWindow that you have created, being displayed the user? – bit Apr 13 '15 at 04: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. – Шыназ Алиш Apr 13 '15 at 04:29
  • 1
    You seem to have used the newly created instance of the MainWindow(), instead try contentMain.Content = new ClubRules(); directly.. – bit Apr 13 '15 at 04:30
  • @bit, As you see, button inside of UserControl. Could you provide me some example? – Шыназ Алиш Apr 13 '15 at 04: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 – bit Apr 13 '15 at 04:37
  • @bit, You are right. I need to find this method, the refenrce to my MainWindow. – Шыназ Алиш Apr 13 '15 at 04:43
  • Now that you understand the issue, you might want to consider changing the design a little bit and put the button in the MainWindow's UI .. – bit Apr 13 '15 at 04:45
  • if you don't care about MVVM nor good practices then make you MainWindow a singleton or pass its instance to every UserControl instance, to make it a singleton look at http://stackoverflow.com/questions/3827612/how-to-make-my-wpf-mainwindow-a-singleton – Xi Sigma Apr 13 '15 at 04:45
  • @bit, Thank you for effect of Kapitan)). I find how to reference in this [post](http://stackoverflow.com/questions/17562358/accessing-mainwindows-controls-from-a-user-control-page-wpf-c-sharp) – Шыназ Алиш Apr 13 '15 at 04:47

2 Answers2

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();
    }
}
sriman reddy
  • 763
  • 8
  • 22
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.

Community
  • 1
  • 1
bit
  • 4,407
  • 1
  • 28
  • 50