【问题标题】:How to do Databinding with Mahapps.Metro WindowCommands and Caliburn Micro?如何使用 Mahapps.Metro WindowCommands 和 Caliburn Micro 进行数据绑定?
【发布时间】:2014-07-30 09:34:38
【问题描述】:

我想在我的 ShellViewModel 中绑定一个 IScreens 列表

    ...

    public ObservableCollection<IScreen> RightWindowCommands
    {
        get
        {
            return this.rightWindowCommands;
        }
        set
        {
            this.rightWindowCommands = value;
            this.NotifyOfPropertyChange(() => this.RightWindowCommands);
        }
    }

    ...

并使用 Mahapps.Metro 窗口中的屏幕列表作为 WindowCommands

<controls:MetroWindow.RightWindowCommands>
    <controls:WindowCommands ItemsSource="{Binding RightWindowCommands}" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ContentControl cal:View.Model="{Binding .}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>           
    </controls:WindowCommands>
</controls:MetroWindow.RightWindowCommands>

它似乎不能以这种方式工作(IScreen 打印为文本而不是解析视图)但如果我将它放入 ListBox 而不是窗口本身内的 MetroWindow.WindowCommands 则它可以工作。 Caliburn.Micro 似乎没有搜索这个可视化树。

有什么建议可以强制 caliburn 解析这个吗?

更新:

日志:调试:未指定 XML 'id' 和 'name' - 使用生成的对象名称 [Shells.MyViewModel#3FB40AD] 日志:信息:未应用操作约定:get_Session 没有可操作的元素。 日志:信息:未应用操作约定:get_Session 没有可操作的元素。 日志:信息:未应用操作约定:句柄没有可操作的元素。 ...

但没有关于解决不起作用的视图或尝试解决视图的问题。

【问题讨论】:

  • 你试过cal:Bind.Model吗?
  • @Charleh 嗯,试过 cal:Bind.Model="ShellViewModel" 但这似乎不是正确的语法...
  • 您需要绑定到 Model 本身,在那个 sn-p 中,您试图将模型绑定到字符串 - 应该是 cal:Bind.Model="{Binding}" - 这将绑定 ObservableCollection&lt;IScreen&gt; 中的每个项目到 ContentControl 使用 caliburn 约定
  • @Charleh 已经尝试过 cal:Bind.Model="{Binding}" 但结果是一样的...视图模型的名称显示为文本,而不是解析并替换为视图跨度>
  • 听起来您的视图模型无法解析 - 您是否有控制台的 ILog 实现?例如在您的引导程序 LogManager.GetLog = (e) =&gt; return new DebugLogger() 中,DebugLogger 是一个简单的记录器,它写入控制台(实现 ILog)。这可以帮助解决绑定问题,但听起来就像ViewModelLocator 没有找到您的视图(通常它会将TextBlock 放置在视图名称而不是实际视图的位置,如果它可以的话'找不到)

标签: c# data-binding caliburn.micro mahapps.metro


【解决方案1】:

我通过使用 ContentControl 而不是使用单独的 ViewModel 而不是使用 DataTemplate 的解决方法(甚至看起来更加解耦)解决了这个问题。 DataTemplate 在 ContentControl 的内容中使用时有效:

<controls:MetroWindow.RightWindowCommands>
    <controls:WindowCommands>
        <controls:MetroContentControl cal:View.Model="{Binding MySeparateViewModel}" />
    </controls:WindowCommands>
</controls:MetroWindow.RightWindowCommands>

cal:View.Model 查看我的 ShellViewModel 的属性。 如果其他人有同样的问题要解决,希望这会有所帮助。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-10-12
  • 1970-01-01
  • 1970-01-01
  • 2011-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多