使用 Caliburn.Micro 将命令绑定到 ListView 内的按钮 [英] Bind a Command to a Button inside a ListView with Caliburn.Micro

查看:15
本文介绍了使用 Caliburn.Micro 将命令绑定到 ListView 内的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建类似 MDI 选项卡式界面的内容,因此我在左侧有一个导航窗格(一个列表框),在右侧有一个 ContentPresenter.

I'm trying to create something like a MDI tabbed Interface so I have a navigation pane (a Listbox) on the left, and a ContentPresenter on the right.

我有一个 ShellViewModel,上面有一个名为 AvailAbleScreens 的 BindableCollection,我成功地使用 ListViews DataTemplate 绑定到该列表:

I have a ShellViewModel that has a BindableCollection on it called AvailAbleScreens and I managed successfully to bind to that list with a ListViews DataTemplate:

<ListView x:Name="AvailableScreens">
    <ListView.ItemTemplate>
        <DataTemplate>
            <WrapPanel>
                <BulletDecorator />
                <Button x:Name="DisplayView">
                    <TextBlock Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}" />
                </Button>
            </WrapPanel>
        </DataTemplate>
    </ListView.ItemTemplate>

现在的问题是,虽然按钮的名称设置正确,但我无法为我触发命令.在 MdiViewModel 类上,我有该按钮的以下代码:

The problem now is that although the Name of the Button is set correctly, I can't make the Command fire for me. On the MdiViewModel class I have the following code for that button:

public bool CanDisplayView()
{
    return true;
}

public void DisplayView()
{
    MessageBox.Show("Hello");
}

所有 Caliburn.Micro 示例都通过约定绑定到 x:Name 属性,但是如果我删除 Text="{Binding}" 它停止工作,所以我怀疑这种数据绑定方式不适用于 sub-楷模?

All the Caliburn.Micro samples work with binding through conventions to the x:Name Property, but if I remove the Text="{Binding}" it stops working so I suspect that this way of databinding doesn't work for sub-models?

无论如何,Shell 的 ViewModel 目前非常简单:

Anyway, the Shell's ViewModel is quite simple at the moment:

ShellViewModel
 * AvailableScreens
    -MdiViewModel1
    -MdiViewModel2
 * CurrentActiveScreen

知道如何使用 Caliburn.Micro 做到这一点吗?Rob Eisenberg 在 Twitter 上向我建议,在进入成熟的 Caliburn 框架之前,我可能想先使用 Caliburn.Micro.

Any Idea how I'd do this with Caliburn.Micro? Rob Eisenberg suggested to me on Twitter I might want to start out with Caliburn.Micro before going into the full fledged Caliburn framework.

推荐答案

遗憾的是,我们无法自动将约定应用于 DataTemplates 的内容.这样做的原因是我们没有办法拦截WPF/Silverlight的模板创建机制.要解决这个问题,您有几个选择:

Unfortunately, we cannot automatically apply conventions to the contents of DataTemplates. The reason for this is that we have no way of intercepting WPF/Silverlight's template creation mechanism. To get around this you have a couple of options:

  1. 不要在数据模板内部使用约定;改用显式绑定和 Message.Attach

  1. Do not use conventions inside of DataTemplates; Use explicit bindings and Message.Attach instead

将所有数据模板提取到 UserControl 中,这将重新启用 UserControl 中的约定.这对于大型模板来说是个好主意,但对于小型模板来说很乏味

Extract all DataTemplates into UserControls which will re-enalbe conventions across the UserControl. This is a good idea for large templates, but tedious for small ones

像这样在 DataTemplate 的根 UIElement 上使用 Bind.Model 附加属性 Bind.Model="{Binding}".这样做会导致约定与 DataTemplate 绑定.不幸的是,由于 Caliburn 中的一些错误,这可能无法正常工作.不过它在 Caliburn.Micro 中确实有效.我希望尽快解决这个错误.

Use the Bind.Model attached property on the root UIElement of the DataTemplate like this Bind.Model="{Binding}". Doing this will cause conventions to be bound against the DataTemplate. Unfortunately, this may not quite work right due to some bugs in Caliburn. It does work in Caliburn.Micro though. I'm hoping to work that bug out soon.

这篇关于使用 Caliburn.Micro 将命令绑定到 ListView 内的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆