Sei
Sei

Reputation: 13

Avalonia/WPF Passing a Router into a ControlTemplate

Long story short, I have an Avalonia project where I'm using the SimpleRouter library to achieve routing. The code is very similar to the full code example, using a ServiceCollection and I can access the HistoryRouter<ViewModelBase> router in my MainViewModel.

I also have a ControlTemplate that is meant to provide reusable functionality for some, but not all of the pages. The functionality includes going to the previous page, which means I'd like to use the router's GoBack() method for this. Unfortunately, I have absolutely no idea how to go about passing the router or the ServiceCollection into the component so I can use the method. Searching the internet has been fairly unhelpful with suggesting what I need to do.

Component example:

<Styles>
    <Style Selector="controls|MyComponent">
        <Setter Property="Template">
            <ControlTemplate>
                <StackPanel>
                    <Button>Go Back</Button>
                    <ContentPresenter Content="{TemplateBinding Content}"/>
                </StackPanel>
            </ControlTemplate>
        </Setter>
    </Style>
</Styles>

PageView example:

<UserControl x:Class="Project.Views.SomePageView">
    <StackPanel>
        <controls:MyComponent/>
        <TextBlock Text="Some Content" />
    </StackPanel>
</UserControl>

What would be the proper way to achieve something like I want without breaking any of the design patterns? Any help would be appreciated.

Upvotes: 0

Views: 36

Answers (0)

Related Questions