绑定命令以一个按钮Caliburn.Micro一个ListView内 [英] Bind a Command to a Button inside a ListView with Caliburn.Micro

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

问题描述

我试图创造一些像MDI标签式界面,所以我必须在左侧导航窗格(列表框),并在右侧的内容presenter。

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.

我有,有一个BindableCollection它称为AvailAbleScreens,我成功地设法绑定到该列表与列表视图的DataTemplate一个ShellViewModel:

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类我有以下的code该按钮:

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绑定工作:名称属性,但如果我删除文本={结合},它停止工作,所以我怀疑,数据绑定的这种方式不适合子工作-楷模?

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?

不管怎样,壳牌视图模型是目前非常简单:

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

ShellViewModel
 * AvailableScreens
    -MdiViewModel1
    -MdiViewModel2
 * CurrentActiveScreen

任何想法我如何与Caliburn.Micro做到这一点?罗布艾森伯格建议我在Twitter上我可能要再进一个全功能的卡利框架,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. 不要使用的DataTemplates内约定;使用显式绑定和Message.Attach而不是

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

提取所有的DataTemplates到用户控件,这将重新enalbe整个用户控件约定。这是一个大的模板好主意,但对于繁琐的小的

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

使用 Bind.Model 附加属性在根的UIElement 中的DataTemplate这样<$的C $ C> Bind.Model ={结合}。这样做将导致对抗的DataTemplate要绑定约定。不幸的是,这可能不是很合适的工作,由于在卡利一些错误。它在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天全站免登陆