how to bind list of Commands (List<ICommand>)
-
Thursday, September 30, 2010 1:57 PM
have a look at the following
public class CommandDefinition
{
public string MenuName { get; set; }
public ICommand MenuCommand { get; set; }
}
public class AdminCommandService
{
public static IEnumerable<CommandDefinition> GetCommands()
{
AdminCommandService objService = new AdminCommandService();
List<CommandDefinition> CommandCollection=new List<CommandDefinition>();
CommandDefinition objInvokeCmd = new CommandDefinition
{
MenuName="Add User",
MenuCommand = new DelegateCommand<object>(objService.InvokeAddUser)
};
CommandDefinition objDelUserCmd = new CommandDefinition
{
MenuName="Delete User",
MenuCommand = new DelegateCommand<object>(objService.DeleteUser)
};
CommandDefinition objUserDetailCmd = new CommandDefinition
{
MenuName=" User Detail",
MenuCommand = new DelegateCommand<object>(objService.UserDetails)
};
CommandCollection.Add(objInvokeCmd);
CommandCollection.Add(objDelUserCmd);
CommandCollection.Add(objUserDetailCmd);
return CommandCollection;
}
public void InvokeAddUser(object parm)
{
}
public void DeleteUser(object parm)
{
}
public void UserDetails(object Parm)
{
}
}---------------------------------------------------------------------------
public class MenuViewViewModel
{
public IEnumerable<CommandDefinition> Commands { get; set; }
public MenuViewViewModel()
{
CommandService objService=SingletonUnityService.Instance.GetByType<CommandService>();
this.Commands = objService.GetCommandsByRole(Roles.Admin);
}
public void OnListClicked(object parm)
{
CommandDefinition cmd = parm as CommandDefinition;
}
}-----------------------------------------------------------
<ListBox x:Name="UserMenu" ItemsSource="{Binding Commands}" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding MenuName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction
Command="{Binding Path=MenuCommand}"
CommandParameter="{Binding SelectedItem,ElementName=UserMenu}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
Menus are getting populated but the event is not triggering...i mean its not calling its event...
it is not calling its action methods...
any ide....
All Replies
-
Thursday, September 30, 2010 3:26 PM
Have you checked the output window for problems?
Also, is this one of the issues with DataTemplate not being a DependencyProperty and you'll need to use something like the ViewModelLocator in MVVVMLight to address this?
<CheckBox x:Name="ChkAAMapIt" Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" Click="ChkAAMapIt_Click" IsChecked="{Binding MapIt, Mode=TwoWay}" > <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <i:InvokeCommandAction Command="{Binding Source={StaticResource Locator}, Path=TruckDetailsViewModel.AlertAlarmClickCommand}" CommandParameter="{Binding}"/> </i:EventTrigger> </i:Interaction.Triggers> </CheckBox>

