Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF MVVM Case: ItemsControl contains hyperlink and command to update property in ViewModel
    primarykey
    data
    text
    <p>I have a case, a little bit more complex but I try to illustrate and do some modification to show the point in a simple way.</p> <p>Let's say I have a Window1 as view and a Window1ViewModel as its viewmodel. I also have a SubMenuViewModel class to represent submenu in the window. I want to make an ItemsControl in Window1 which contains many submenus. Every time user clicks one of the submenu, the property CurrentSubMenu updates to the corresponding submenu. This is the problem, I can't manage to update the CurrentSubMenu in the <em>Window1ViewModel</em>.</p> <p>.</p> <p><strong>SubMenuViewModel :</strong> </p> <pre><code>public class SubMenuViewModel : INPC { private string _submenuname; public string SubMenuName { get { return _submenuname; } set { _submenuname = value; RaisePropertyChanged("SubMenuName"); } } private string _displayname; public string DisplayName { get { return _displayname; } set { _displayname = value; RaisePropertyChanged("DisplayName"); } } // Command for Hyperlink in ItemsControl // private RelayCommand _commandSubmenu; public RelayCommand CommandSubMenu { get { return _commandSubmenu; } set { _commandSubmenu = value; RaisePropertyChanged("CommandSubMenu"); } } // end of command public SubMenuViewModel(string Submenu_name, string Display_name) { _submenuname = Submenu_name; _displayname = Display_name; } } </code></pre> <p><strong>Window1 :</strong></p> <pre><code>&lt;Window x:Class="SomeProject.Window1" xmlns:vm="clr-namespace:SomeProject.ViewModel" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="This is Some Project" WindowState="Maximized"&gt; &lt;Window.DataContext&gt; &lt;vm:Window1ViewModel/&gt; &lt;/Window.DataContext&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="*"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="250" /&gt; &lt;ColumnDefinition Width="*"/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Border Name="SubMenuPanelBorder" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="15 0 15 0" BorderThickness="2" BorderBrush="Black" Padding="10 10"&gt; &lt;HeaderedContentControl Content="{Binding Path=SubMenus}" Header="Panel Submenu :"&gt; &lt;HeaderedContentControl.ContentTemplate&gt; &lt;DataTemplate&gt; &lt;ItemsControl ItemsSource="{Binding}" Foreground="White" Background="Transparent" Margin="0 15"&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock&gt; &lt;Hyperlink Command="{Binding Path=CommandSubMenu}"&gt; &lt;TextBlock Margin="0 5" Text="{Binding Path=DisplayName}"/&gt; &lt;/Hyperlink&gt; &lt;/TextBlock&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; &lt;/DataTemplate&gt; &lt;/HeaderedContentControl.ContentTemplate&gt; &lt;/HeaderedContentControl&gt; &lt;/Border&gt; ....... ....... &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p><strong>Window1ViewModel :</strong></p> <pre><code>class Window1ViewModel : INPC { private List&lt;SubMenuViewModel&gt; _submenus; public List&lt;SubMenuViewModel&gt; SubMenus { get { return _submenus; } set { _submenus = value; RaisePropertyChanged("SubMenus"); } } private SubMenuViewModel _currentSubMenu; public SubMenuViewModel CurrentSubMenu { get { return _currentSubMenu; } set { _currentSubMenu = value; RaisePropertyChanged("CurrentSubMenu"); } } public Window1ViewModel() { SubMenus = MenuDefinition.GetSubMenus(); /*** Set the SubMenus's command to update the CurrentSubMenu, and == HERE IS WHERE I GOT LOST ==. The CurrentSubMenu is always point to the last submenu in the loop when clicked. By the way, the reason I use loop is because the submenu in the Menu definition sometimes changed and there are many number of submenus there, and there are some other reasons (I use Menu-submenu mechanism, but it's not essential to show it here because it's not the main point I want to show). So, finally I use loop instead of specifying each command one by one. ***/ foreach(SubMenuViewModel submenu in SubMenus){ submenu.CommandSubMenu=new RelayCommand(()=&gt;clickedSubMenu(submenu.SubMenuName)); } } public void clickedSubMenu(string submenu_name) { CurrentSubMenu = SubMenus.Find(sub =&gt; sub.SubMenuName == submenu_name); } } </code></pre> <p><strong>MenuDefinition :</strong></p> <pre><code>public static List&lt;SubMenuViewModel&gt; GetSubMenus() { return new List&lt;SubMenuViewModel&gt; { new SubMenuViewModel("overview_product", "Overview Products"), new SubMenuViewModel("search_product","Search Products"), new SubMenuViewModel("update_product","Update Data Products"), new SubMenuViewModel("order_product","Order Products"), new SubMenuViewModel("supplier_product","Products Supplier"), ................. ................. ................. }; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload