【问题标题】:View is not being resolved with Toolbar ItemsSource使用 Toolbar ItemsSource 无法解析视图
【发布时间】:2012-06-26 02:07:49
【问题描述】:

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

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

这是我所拥有的:

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
{

}

【问题讨论】:

    标签: c# .net caliburn.micro caliburn


    【解决方案1】:

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

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

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

    <ListBox x:Name="ToolbarPlugins"/>
    

    SinglePaintToolView 按钮按预期显示。

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

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

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

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

    【讨论】:

    • 是的,看来这就是我必须做的才能使用工具栏。但是,ListBox 解决方案对我来说非常有用。当我有时间时,我会在 ControlTemplate 上进行更多研究。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 2020-10-27
    • 1970-01-01
    • 2014-08-27
    相关资源
    最近更新 更多