Шыназ Алиш
Шыназ Алиш

Reputation: 399

How to change UserControl inside of ContentControl in wpf c# programmatically

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.

Upvotes: 2

Views: 5251

Answers (2)

sriman reddy
sriman reddy

Reputation: 773

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();
    }
}

Upvotes: 3

bit
bit

Reputation: 4487

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.

Upvotes: 1

Related Questions