我有一個WPF應用程序,我試圖實現MVVM。主窗口具有視圖模型和自定義用戶控件。此用戶控件包含一個文本框和一個按鈕,並從主父窗口共享數據上下文。一切都很好...WPF,MVVM和嵌套UserControl的事件處理
我已經能夠將文本框綁定到viewmodel中的一個變量,並且它能夠成功運行。但是...對於我的生活,我無法將按鈕上的事件處理程序綁定到視圖模型。我不斷收到以下錯誤:
Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'.
我知道這是新手到MVVM的一個常見問題。我搜索了網頁,似乎沒有任何工作。任何人都可以解釋我做錯了什麼?
下面是一些示例代碼: 主窗口:
<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 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>
我不認爲Click屬性應該使用Binding這個詞,就像Click =「OnClick」 – 2013-04-26 20:55:29