带菜单的 WPF Caliburn 微型导体
WPF Caliburn Micro Conductors with Menu
我目前正在使用 Caliburn Micro 2.0.2 尝试使用 ShellView 顶部的菜单进行 Conductor 导航。 MenuItem 的第一级工作正常,但第二级抛出 "No target found for method Select." 异常。
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 方法。我不知道如何让事件进行连接。请帮忙
我成功了。绑定到 RelativeSource Self 是问题所在。我现在可以按名称绑定到元素。这也让我在 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();
}
}
}
}
我目前正在使用 Caliburn Micro 2.0.2 尝试使用 ShellView 顶部的菜单进行 Conductor 导航。 MenuItem 的第一级工作正常,但第二级抛出 "No target found for method Select." 异常。
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 方法。我不知道如何让事件进行连接。请帮忙
我成功了。绑定到 RelativeSource Self 是问题所在。我现在可以按名称绑定到元素。这也让我在 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();
}
}
}
}