0
votes

I'm trying to build a small navigation system in my WPF application. I'm using this tutorial to navigate between pages. I want to add 'Go back' functionality on top of it for one UserControl.

I have a UserControl Orders and another UserControl Order. Orders is shown in MainWindow and when I click on an button, Order UserControl should be shown in the same place in MainWindow. I tried to put a reference to the Orders usercontrol in the Order usercontrol and navigate to the Orders through Order. But the Order isn't destroyed since I'm using a variable from that class.

How can I make sure that when I navigate to Order form Orders, the Orders isn't destroyed and when I navigate to Orders from Order, Order is destroyed.

Button click event handler in Orders Class:

private void ShowOrder(object sender, RoutedEventArgs e)
{
    Order order = new Order();
    Switcher.Switch(order);
}

Return back button click handler in Order Class

public UserControl parent;

private void ReturnBack(object sender, RoutedEventArgs e)
{
    Switcher.Switch(parent);
}
1

1 Answers

0
votes

I usually do the next pattern whice uses ControlTemplate, lets say you have in your class:

private Enums.View _currView;
public Enums.View CurrView 
{
    get
    {
        return _currView;
    }
    set
    {
        _currView = value;
        OnPropertyChanged("CurrView");
    }
}

When Enums.View is:

public enum View
{
    ViewA = 1,
    ViewB = 2,
    ViewC = 3,
}

Then, using Binding to CurrView above we change the view when it changes:

<UserControl ...
         xmlns:Views="clr-namespace:CustomersManager.View"
         d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.Resources>
        <!--*********** Control templates ***********-->
        <ControlTemplate x:Key="DefultTemplate">
            <Views:DefultCustomersView/>
        </ControlTemplate>
        <ControlTemplate x:Key="A">
            <Views:ViewAllCustomersView />
        </ControlTemplate>
        <ControlTemplate x:Key="B">
            <Views:AddNewCustomersView />
        </ControlTemplate>
        <ControlTemplate x:Key="C">
            <Views:EditCustomersView />
        </ControlTemplate>
    </UserControl.Resources>

    <Border BorderBrush="Gray" BorderThickness="2">
        <Grid>
            <ContentControl DataContext="{Binding}" >
                <ContentControl.Style>
                    <Style TargetType="ContentControl">
                        <Setter Property="Template" Value="{StaticResource DefultTemplate}" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=CurrView}" Value="ViewA">
                                <Setter Property="Template" Value="{StaticResource A}" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Path=CurrView}" Value="ViewB">
                                <Setter Property="Template" Value="{StaticResource B}" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Path=CurrView}" Value="ViewC">
                                <Setter Property="Template" Value="{StaticResource C}" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ContentControl.Style>
            </ContentControl >
        </Grid>  
    </Border> 
</UserControl>