I'm trying to make a WPF MVVM application where there is one main window that has a sidebar with a button for each MVVM view and the rest of said window displays one view at a time.
(ignore that in this example the sidebar is at the top, this is just for illustrative purposes)
My goal is to be able to click a button and have the yellow part change to the view that corresponds with the button.
I can do it just fine hardcoded-ly like so:
<Button Command={Binding ChangeView} CommandParameter={Binding CalculatorView}/>
public FrameworkElement CurrentControlView { get; set; }
public ICommand ChangeViewCommand { get; }
// command initialization
void ChangeView(string viewName)
{
Type viewType = Type.GetType($"Program.Views.{viewName}");
CurrentControlView = (FrameworkElement)Activator.CreateInstance(viewType);
}
And bind what view is being shown via the CurrentControlView
property.
However I'm trying to make it build itself. What I mean is, I'd like to write code that will look for all MVVM-convention views (XAML files with codebehinds) in a certain namespace and create a button that, when clicked, would fire code that would show its corresponding view.
So far I thought about using some sort of reflection code to gather the views, put them in a collection and have an ItemsControl
bind to that collection something like so:
(again keep in mind in the picture the buttons are at the top but I want them at the left side)
<ItemsControl ItemsSource="{Binding ViewsCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="??" Command="{Binding ChangeViewCommand}" CommandParameter="??" HorizontalAlignment="Center" VerticalAlignment="Center" Height="40"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
However here I'm not sure what to put for Content
as I want the button to display the name of the view. So if I have "CalculatorView.xaml" I'd trim the "View" part and the button would be like in the picture with content "Calculator".
Also I don't know how to pass the view name for each separate button for the "CommandParameter". Even passing the button content would suffice, as I can handle it from there.
How can I achieve this?