我正在尝试使用Caliburn Micro自学WPF,MVVM。到目前为止一切进展顺利,但是当 View 中的comboBox的选择更改时,试图在ViewModel中触发事件时遇到了问题。
这是我的最高观点(在Xaml中):
<Window x:Class="Translator.Views.TranslatorView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:model="clr-namespace:Translator.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=model:TranslatorViewModel}"
Title="Translator" Height="269.301" Width="1030.263"
xmlns:cal="http://www.caliburnproject.org">
这是我的ComboBox的Xaml:
<ComboBox Name="StoreCombo" Grid.Column="3" Grid.Row="0" Margin="10" Height="25" SelectedValue="{Binding Type}"
cal:Message.Attach="[Event SelectionChanged] = [StoreSelectionChanged]"
cal:Action.Target="{Binding ElementName=StoreCombo, Path=DataContext}">
</ComboBox>
这是我在ViewModel中的事件:
public void StoreSelectionChanged(object sender, SelectionChangedEventArgs args)
{
}
但是,在运行应用程序并更改选择内容时出现此异常:
“WindowsBase.dll中发生了'System.Exception'类型的未处理的异常
附加信息:未找到方法StoreSelectionChanged的目标。
如果存在用于此异常的处理程序,则可以安全地继续执行该程序。”
我已经尝试过用谷歌搜索,但是到目前为止,我还不知道该怎么做。
有人可以帮我吗?
非常感谢
最佳答案
<ComboBox Name="StoreCombo" SelectedValue="{Binding Type}"
cal:Message.Attach="[Event SelectionChanged] = [StoreSelectionChanged]"
cal:Action.Target="{Binding ElementName=StoreCombo, Path=DataContext}">
</ComboBox>
(1):目标已经是DataContext,因此只需删除这行就可以了。
cal:Action.Target="{Binding ElementName=StoreCombo, Path=DataContext}"
(2)Caliburn Cheat Sheet,如果您不需要(Sender,EventArgs)
cal:Message.Attach="[Event SelectionChanged] = [Action StoreSelectionChanged]"
(2.1)如果要 eventargs :
cal:Message.Attach="[Event SelectionChanged] = [Action StoreSelectionChanged($eventArgs)]"
(2.2)如果要发送者和 eventargs
cal:Message.Attach="[Event SelectionChanged] = [Action StoreSelectionChanged($this,$eventArgs)]"
关于wpf - wpf caliburn ComboBox OnSelectionChanged事件异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25219791/