使用Caliburn Micro

时间:2016-05-19 03:56:12

标签: c# caliburn.micro

我有ItemsControl如下:

    <ItemsControl ItemsSource="{Binding Items}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ContentControl cal:Message.Attach="[Event MouseDoubleClick] = [Action ($dataContext).Launch]">
                    <Grid Background="LightSteelBlue" Width="100" Height="100" Margin="4"/>
                </ContentControl>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

ItemsControl项目来源与视图模型中的ObservableCollection<Item>绑定。

以下是Item

的定义
public class Item
{
    public void Launch()
    {
    }
}

双击Item时,我尝试使用以下语法触发Launch()的{​​{1}}方法:

Item

但是我得到了&#34;没有找到方法&#34;错误信息。

我是否正确使用了语法?或者速记不支持这样的用例?

更新:

我的观点模型如下:

cal:Message.Attach="[Event MouseDoubleClick] = [Action ($dataContext).Launch]"

当我尝试使用以下语法触发public class MainViewModel { // Binding Properties. public ObservableCollection<Item> Items { get; set; } public void Launch() { } // ctor. public MainViewModel() { Items = new ObservableCollection<Item>(); Items.Add(new Item()); Items.Add(new Item()); } } 的{​​{1}}方法时,它会触发视图模型的Launch()方法。

Item

1 个答案:

答案 0 :(得分:0)

当我从视图模型中删除Launch()方法时,会触发Launch() Item方法。看起来如果在视图模型中找到匹配的方法,框架将在视图模型中执行该特定方法。如果在视图模型中找不到任何内容,则框架在其当前上下文中搜索并在找到匹配方法时执行该方法。