I have a WPF application and am trying to implement MVVM. The main window has a viewmodel and a user control. This user control contains a text field and a button and separates the datacontext from the main parent window. Things are good...
I managed to bind a text field to a variable within the viewmodel, and it works successfully. But ... for the life of me, I cannot associate an event handler with a button on the viewmodel. I keep getting the following error:
Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'.
I know this is a common problem with new ones for MVVM. I am combing the network and nothing is working. Can anyone explain what I'm doing wrong?
Here is a sample code: Main window:
<Window x:Class="Mvvm.View.MainWindowView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:view="clr-namespace:ZeeZor.Kiosk.Mvvm.View" WindowStyle="None" WindowState="Maximized" > <Grid > <view:ControlPanelView Margin="5, 0, 5, 0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" > </view:ControlPanelView> </Grid> </Window>
UserControl:
<UserControl x:Class="Mvvm.View.ControlPanelView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > <Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <TextBlock Grid.Row="0" TextAlignment="Right" Text="{Binding Total}" Margin="0,5,20,5" /> <Button Grid.Row="1" Content="Test" Click="{Binding Path=OnClick}" Width="220" Height="100"/> </Grid> </UserControl>
source share