Using MVVMLight on WinRT, Command does not relay properly when the binding is declared in XAML
I was facing an issue where a Button doesn't fire off the Command when the binding is declared in XAML like such:
1 <UserControl x:Class="BOF.Win8Metro.MainPage" 2 xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="https://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006" 6 mc:Ignorable="d" 7 d:DesignHeight="768" 8 d:DesignWidth="1366" 9 xmlns:converters="using:BOF.Win8Metro.Converters" 10 DataContext="{Binding Main, Source={StaticResource Locator}}"> 11 12 <!--’¦--> 13 14 <Button x:Name="AddCustomer" Height="120" Width="120" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="20" 15 Command="{Binding DataContext.RefreshCommand}"> 16 <Grid> 17 <TextBlock FontSize="21" Margin="7" VerticalAlignment="Bottom" ><Run Text="Refresh"/></TextBlock> 18 </Grid> 19 </Button> 20
Not that: On line 10, I've setup the datacontext to the MainViewModel using MVVMLight.
On line 14, I'm binding the button to the RefreshCommand on the MainViewModel.
1 RefreshCommand = new RelayCommand(Refresh);
On the Main View Model, I set up the relay as above. Interestingly enough, the Refresh delegate wasn't called.
I finally found that a Command in WinRT needs to have a parameter of object. And since the Command binding on Xaml is done using some sort of relflection, it ignores the command if the exact signature wasn't found. So to fix it, I have to change the Relay Command to the following:
1 RefreshCommand = new RelayCommand<object>(Refresh);