我目前正在使用Caliburn Micro 2.0.2尝试在我的ShellView顶部使用Menu进行导体导航。 MenuItem的第一级工作正常,但第二级抛出"没有找到方法选择的目标。"例外。
ShellView代码:
<Grid xmlns:cal="http://www.caliburnproject.org"
xmlns:cal2="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Menu VerticalAlignment="Top" ItemsSource="{Binding Items}">
<Menu.Resources>
<Style TargetType="MenuItem">
<Setter Property="cal:Message.Attach"
Value="[Event Click] = [Action Select($DataContext)]" />
<Setter Property="cal:Action.TargetWithoutContext"
Value="{Binding DataContext, RelativeSource={RelativeSource Self}}}"/>
</Style>
<HierarchicalDataTemplate ItemsSource="{Binding Items}"
DataType="{x:Type cal2:Screen}">
<TextBlock Text="{Binding DisplayName}"/>
</HierarchicalDataTemplate>
</Menu.Resources>
</Menu>
<ContentControl Grid.Row="1" cal:View.Model="{Binding ActiveItem}"/>
</Grid>
ShellViewModel代码:
public class ShellViewModel : Conductor<IScreen>.Collection.OneActive
{
public ShellViewModel(IEnumerable<IScreen> viewmodels)
{
//omitted for brevity.
}
public void Select(object datacontext)
{
var vm = datacontext as IScreen;
if (vm != null && Items.Contains(vm))
{
ActivateItem(vm);
}
}
}
具有更多视图模型的内部Conductor具有与相应接口类型相同的select方法。我无法弄清楚如何将事件连接起来。请帮忙。
答案 0 :(得分:1)
我得到了它的工作。绑定到RelativeSource Self是个问题。我现在已经按名称绑定了Element。这也允许我在shell上只有一个方法。
ShellView代码:
<Grid xmlns:cal="http://www.caliburnproject.org"
xmlns:cal2="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Menu x:Name="menu" VerticalAlignment="Top" ItemsSource="{Binding Items}">
<Menu.Resources>
<Style TargetType="MenuItem">
<Setter Property="cal:Message.Attach"
Value="[Event Click] = [Action Select($DataContext)]" />
<Setter Property="cal:Action.TargetWithoutContext"
Value="{Binding DataContext, ElementName=menu}"/>
</Style>
<HierarchicalDataTemplate ItemsSource="{Binding Items}"
DataType="{x:Type cal2:Screen}">
<TextBlock Text="{Binding DisplayName}"/>
</HierarchicalDataTemplate>
</Menu.Resources>
</Menu>
<ContentControl Grid.Row="1" cal:View.Model="{Binding ActiveItem}"/>
</Grid>
ShellViewModel代码:
public class ShellViewModel : Conductor<IScreen>.Collection.OneActive
{
public ShellViewModel(IEnumerable<IScreen> viewmodels)
{
//omitted for brevity.
}
public void Select(object datacontext)
{
var vm = datacontext as IScreen;
if (vm != null && Items.Contains(vm))
{
if (vm.IsActive)
return;
ActivateItem(vm);
vm.Refresh();
}
else
{
var vm2 = datacontext as Screen;
if (vm2 != null)
{
(vm2.Parent as IConductActiveItem)?.ActivateItem(vm2);
vm2.Refresh();
}
}
}
}