c# - View 未使用 Toolbar ItemsSource 解析

标签 c# .net caliburn.micro caliburn

当我的 SinglePaintToolbarView 被绑定(bind)为 ShellView 工具栏中的按钮列表时,Caliburn Micro 框架似乎并没有检索它。我希望按钮在添加到工具栏时只显示其文本内容。但是,相反,我得到了这个:

工具栏中似乎没有任何可点击的按钮。我知道我的插件已成功加载,因为我能够将列表中的插件之一绑定(bind)为 ContentControl 并且出现了 View 。当我尝试在工具栏中绑定(bind)插件列表时,它似乎不起作用。

这是我的:

ShellView.xaml

<UserControl x:Class="Starbolt.Views.ShellView"
         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:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <ToolBarTray>
            <ToolBar ItemsSource="{Binding Path=ToolbarPlugins}"/>
        </ToolBarTray>
    </Grid>
</UserControl>

ShellViewModel.cs

[Export(typeof(IShell))]
public class ShellViewModel : PropertyChangedBase, IShell
{

    [ImportMany(typeof(IToolbarPlugin))]
    private IEnumerable<IToolbarPlugin> _toolbarPlugins = null;

    public IEnumerable<IToolbarPlugin> ToolbarPlugins { get { return _toolbarPlugins; } }
}

SinglePaintToolbarView.xaml

<UserControl x:Class="Starbolt.Plugin.SinglePaintTool.Views.SinglePaintToolView"
         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:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="128" d:DesignWidth="32">
    <Button Name="btnSinglePaintTool" Content="Single Paint Tool" Width="128" Height="32"/>
</UserControl>

SinglePaintToolViewModel.cs

[Export(typeof(IToolbarPlugin))]
public class SinglePaintToolViewModel : IToolbarPlugin
{

}

最佳答案

基本上,您的设计似乎可行。如果你更换

<ToolBarTray>
    <ToolBar x:Name="ToolbarPlugins"/>
</ToolBarTray>

(请注意,您不需要显式绑定(bind) ItemsSource,您也可以使用 Caliburn Micro 属性名称约定):

<ListBox x:Name="ToolbarPlugins"/>

SinglePaintToolView 按钮按预期显示。

我怀疑问题出在 ToolBar ControlTemplate ,这肯定比 ListBox ControlTemplate 更能限制工具栏项目的布局。

所以我的猜测是,如果您真的想使用 ToolBar 控件来显示您的 IToolbarPlugin View ,您可能必须设计一个专用的 ToolBar 项目中的控件模板。

或者,您可以使用例如实现工具栏替换列表框。这可能是一个开始:

<ListBox x:Name="ToolbarPlugins">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

关于c# - View 未使用 Toolbar ItemsSource 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11199898/

相关文章:

c# - 使用 asp-ajax 按顺序加载项目并提高 Web 应用程序速度

c# - Spring.NET 无法转换为类型 Spring.Collections.Set

.net - 按 10 的幂缩放十进制值

c# - Caliburn.Micro - 我可以处理一个 Action 但继续将它发送到可视化树上吗?

c# - 测量下载速度

c# - 使用 AForge.NET 和 Windows 窗体流式传输实时视频有哪些方法?

c# - 没有使用 Application_Error 记录错误

.net - 属性与评论

mvvm - Caliburn.Micro + Unity 2.1 + MVVM : example code

mvvm - Caliburn.Micro、MVVM 和业务层