控制控制绑定

时间:2013-03-06 18:19:20

标签: wpf user-controls caliburn.micro

确定。所以我有一个主视图,其中包含以下行

<!-- The first one is commented cause I tried it and it failed harder -->
<!--<ContentControl Name="BcModel" Grid.Column="2" Width="300" Height="300"/>-->
<local:BigCalendarDateView x:Name="BcModel" Grid.Column="2" Width="300" Height="300"/>

ViewModel

[Export(typeof(AppViewModel))]
public class AppViewModel : Screen
{
    public BigCalendarDateViewModel BcModel { get; set; }

    [ImportingConstructor]
    public AppViewModel(BigCalendarDateViewModel mod, IEventAggregator events)
    {
        this.DisplayName = "Calendar";
        events.Subscribe(this);
        BcModel = mod;
    }
}

对于BigCalendarDateViewModel,我有视图

<!-- Commented because I tried this also -->
<!--cal:Message.Attach="[Event Click] = [Action ChangeState]"-->
<Button Name="ChangeState" Content="Button" HorizontalAlignment="Left" Margin="76,156,0,0" Grid.Row="2" VerticalAlignment="Top" Width="138"/>

在ViewModel中

[Export(typeof(BigCalendarDateViewModel))]
public class BigCalendarDateViewModel : PropertyChangedBase
{
    ...
    public void ChangeState()
    {
        Console.Beep();
    }
}

我可以通过调试,AppView,AppViewModel,BigCalendarDateView和BigCalendarDateViewModel都被初始化,BigCalendarDateViewModel以可视方式显示,但是当我点击按钮时,它要么说它找不到方法,要么就是'做任何事。

看起来BigCalendarDateViewModel似乎永远不会连接到BigCalendarDateView。

当我为控件创建一个引导程序时,它会在自己的窗口中弹出它,它工作正常(在新窗口中,而不是在第一个窗口中)

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我认为您可以通过执行以下操作明确地向视图提供viewmodel:

<local:BigCalendarDateView Grid.Column="2" Width="300" Height="300" cal:Bind.Model={Binding BcModel} />