Hi Team,
I am very new to WPF world and we are designing an application which will be having multiple menus and sub menus. As the application is bigger and having lot more functionality we are using Modular approach defined by PRISM along with complete use of MVVM(zero code behind). We are using Telerik controls for rich UI and for design of the Menu we are using Navigation View(Hamburger Menu). While designing such architecture we are facing many issues as follows:
1. How to bind command to the Navigation View Item events for MVVM prism architecture? I have tried many ways to achieve this but no success(Approaches are mentioned below the questions).
2. How to navigate and load different pages or user control on click of the Navigation View Items as our pages or user controls are present in separate application?(PRISM Modular approach)
3. Want to load this contents in ContentControl Prism Region Manager at runtime.
I have gone through the documentation of Rad Navigation View but not found any ways to achieve the above issues.
Below is the approach we tried to bind command.
1. Approach One : Tried setting in Style with property setter at both ItemStyle and ItemBaseStyle but both are not working.
<p><UserControl.Resources><br> <telerik:StringToGlyphConverter x:Key="StringToGlyphConverter" /><br> <telerik:NullToVisibilityConverter x:Key="NullToVisibilityConverter" /><br> <Style x:Key="ItemPreviewStyle" TargetType="telerik:RadNavigationViewItem"><br> <Setter Property="IconTemplate"><br> <Setter.Value><br> <DataTemplate><br> <telerik:RadGlyph Glyph="{Binding Converter={StaticResource StringToGlyphConverter}}"<br> HorizontalAlignment="Center" VerticalAlignment="Center"/><br> </DataTemplate><br> </Setter.Value><br> </Setter><br> </Style></p><p> <Style x:Key="ItemBaseStyle" TargetType="telerik:RadNavigationViewItem" BasedOn="{StaticResource ItemPreviewStyle}"><br> <Setter Property="Content" Value="{Binding Title}" /><br> <Setter Property="ItemsSource" Value="{Binding SubItems}"/><br> <Setter Property="Icon" Value="{Binding Icon}" /><br> <Setter Property="IconVisibility" Value="{Binding Icon, Converter={StaticResource NullToVisibilityConverter}}"/><br> </Style><br> <Style x:Key="ItemStyle" TargetType="telerik:RadNavigationViewItem" BasedOn="{StaticResource ItemBaseStyle}"> <Setter Property="ItemContainerStyle" Value="{StaticResource ItemBaseStyle}"/><br> <Setter Property="CommandTarget" Value="{Binding ElementName=RadTimeline}"/><br> <Setter Property="Command" Value="{Binding NavigateCommand}"/><br> <Setter Property="CommandParameter" Value="{Binding Title}"/></p><p></Style></p><p> <DataTemplate x:Key="TransitionControlContentTemplate"></p><p><TextBlock Text="{Binding Title}" FontSize="40"></TextBlock></p><p></DataTemplate><br> </UserControl.Resources></p>2. Approach Two : Tried with Telerik event Command Behavior Event Bindings.
<telerik:EventToCommandBehavior.EventBindings><br> <telerik:EventBinding Command="{Binding NavigateCommand}" EventName="MouseLeftButtonDown" CommandTarget="{Binding Title}"/><br> </telerik:EventToCommandBehavior.EventBindings>3. Tried in Telerik radNavigationView.Item it is also not working
<telerik:RadNavigationView.Items><br> <telerik:RadNavigationViewItem Command="{Binding NavigateCommand}"CommandParameter="{Binding Title}"><br> </telerik:RadNavigationViewItem><br> </telerik:RadNavigationView.Items>We are able to show the static value like title with the below code :
<p><telerik:RadNavigationView ItemsSource="{Binding Items}" x:Name="RadTimeline" BorderBrush="LightGreen" BorderThickness="1"<br> ItemContainerStyle="{StaticResource ItemStyle}" ScrollViewer.CanContentScroll="True" ><br> <br> <telerik:EventToCommandBehavior.EventBindings><br> <telerik:EventBinding Command="{Binding NavigateCommand}" EventName="MouseLeftButtonDown" CommandTarget="{Binding Title}"/><br> </telerik:EventToCommandBehavior.EventBindings> </p><p><telerik:RadNavigationView.Content><br> <telerik:RadTransitionControl Content="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType=telerik:RadNavigationView}}" ContentTemplate="{StaticResource TransitionControlContentTemplate}" Foreground="Black" Duration="0:0:0.4"><br> <telerik:RadTransitionControl.Transition><br> <telerik:FadeTransition /><br> </telerik:RadTransitionControl.Transition><br> </telerik:RadTransitionControl><br> </telerik:RadNavigationView.Content></p><p></telerik:RadNavigationView></p>But instead of the RadNavigation View Content we wanted to use Content Control to load dynamic controls using MVVM and Prism like this.
<ContentControl prism:RegionManager.RegionName="ContentRegion" Margin="1,3,3,3"><br> </ContentControl>Below is my ViewModel Code.
P<code style="color: rgb(0, 0, 0);">ublic class MenuControlViewModel : BindableBase<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> {<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> private readonly IRegionManager _regionManager;<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> private List<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">MenuControlModel</code><code style="color: rgb(0, 0, 0);">> items;<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> public List<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">MenuControlModel</code><code style="color: rgb(0, 0, 0);">> Items<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> {<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> get<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> {<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> return this.items;<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> }<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> set<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> {<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> if (this.items != value)<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> {<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> this.items = value;<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> }<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> }<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> }<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">><</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> public MenuControlViewModel(IRegionManager regionManager)<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> {<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> _regionManager = regionManager;<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> this.items = this.GetItems();<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> NavigateCommand = new DelegateCommand<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">string</code><code style="color: rgb(0, 0, 0);">>(Navigate);<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> }<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">><</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> private void Navigate(string navigatePath)<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> {<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> if (navigatePath != null)<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> _regionManager.RequestNavigate("ContentRegion", navigatePath);<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> }<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">><</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> private List<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">MenuControlModel</code><code style="color: rgb(0, 0, 0);">> GetItems()<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> {<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> var Menu-1 = new MenuControlModel() { Icon = "", Title = "Menu-1" };<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> Menu-1.SubItems = new ObservableCollection<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">MenuControlModel</code><code style="color: rgb(0, 0, 0);">><</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> {<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Icon = "", Title = "SubMenu-1" },<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Icon = "", Title = "SubMenu-2" },<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Icon = "", Title = "SubMenu-3" },<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Icon = "", Title = "SubMenu-4" }<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> };<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">><</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> var Menu-2 = new MenuControlModel() { Icon = "", Title = "Menu-2" };<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> Menu-2.SubItems = new ObservableCollection<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">MenuControlModel</code><code style="color: rgb(0, 0, 0);">><</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> {<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Title = "SubMenu-1" }<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Title = "SubMenu-2" },<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Title = "SubMenu-3" },<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Title = "SubMenu-4" },<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Title = "SubMenu-5" },<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> };<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> var Menu-3 = new MenuControlModel() { Icon = "", Title = " Menu-3" };<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> Menu-3.SubItems = new ObservableCollection<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">MenuControlModel</code><code style="color: rgb(0, 0, 0);">><</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> {<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Title = "SubMenu-1"},<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Title = "SubMenu-2"}<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> };<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> var Menu-4 = new MenuControlModel() { Icon = "", Title = " Menu-4"<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> Menu-4.SubItems = new ObservableCollection<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">MenuControlModel</code><code style="color: rgb(0, 0, 0);">><</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> {<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Title = "SubMenu-1" },<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Title = "SubMenu-2" },<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Title = "SubMenu-3" }<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> };<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> var Menu-5 = new MenuControlModel() { Icon = "", Title = " Menu-5" };<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> Menu-5.SubItems = new ObservableCollection<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">MenuControlModel</code><code style="color: rgb(0, 0, 0);">><</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> {<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Title = "SubMenu-1" },<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Title = "SubMenu-2" },<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Title = "SubMenu-3" },<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Title = "SubMenu-4" },<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Title = "SubMenu-5" }<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> };<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">><</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> return new List<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">MenuControlModel</code><code style="color: rgb(0, 0, 0);">><</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> {<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> Menu-1,<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> Menu-2,<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> Menu-3,<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> Menu-4,<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> Menu-5,<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Icon = "", Title = "Menu-6"},<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Icon = "", Title = "Menu-7"},<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> new MenuControlModel() { Icon = "", Title = "Menu-8"},<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> };<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> }<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> public DelegateCommand<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">string</code><code style="color: rgb(0, 0, 0);">> NavigateCommand { get; private set; }<</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">><</code><code style="color: rgb(0, 102, 153); font-weight: bold;">br</code><code style="color: rgb(0, 0, 0);">> }</code>Kindly Help us to achieve this.
