使用 Caliburn Micro 在 ItemsControl 中触发项目的方法
Trigger method of the item within the ItemsControl using 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
时,我尝试使用以下语法触发 Item
的 Launch()
方法:
cal:Message.Attach="[Event MouseDoubleClick] = [Action ($dataContext).Launch]"
但是我收到 "No target found for method" 错误消息。
我使用的语法正确吗?或者 shorthand 不支持这样的用例?
更新:
我的视图模型如下:
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());
}
}
当我尝试使用以下语法触发 Item
的 Launch()
方法时,它触发了视图模型的 Launch()
方法。
cal:Message.Attach="[Event MouseDoubleClick] = [Action Launch]"
当我从我的视图模型中删除 Launch()
方法时,Item
的 Launch()
方法被触发。似乎如果在视图模型中找到匹配的方法,框架将在视图模型中执行该特定方法。如果在视图模型中找到 none,则框架在其当前上下文中搜索并在找到匹配方法时执行该方法。
我有一个 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
时,我尝试使用以下语法触发 Item
的 Launch()
方法:
cal:Message.Attach="[Event MouseDoubleClick] = [Action ($dataContext).Launch]"
但是我收到 "No target found for method" 错误消息。
我使用的语法正确吗?或者 shorthand 不支持这样的用例?
更新:
我的视图模型如下:
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());
}
}
当我尝试使用以下语法触发 Item
的 Launch()
方法时,它触发了视图模型的 Launch()
方法。
cal:Message.Attach="[Event MouseDoubleClick] = [Action Launch]"
当我从我的视图模型中删除 Launch()
方法时,Item
的 Launch()
方法被触发。似乎如果在视图模型中找到匹配的方法,框架将在视图模型中执行该特定方法。如果在视图模型中找到 none,则框架在其当前上下文中搜索并在找到匹配方法时执行该方法。