简体   繁体   English

无法在Caliburn.Micro中以编程方式绑定视图

[英]Cannot bind view programmatically in Caliburn.Micro

I checked out the following thread and followed the example to try and bind a view dynamically: Caliburn.Micro: Create and Bind View programmatically 我签出了以下线程,并按照示例尝试尝试动态绑定视图: Caliburn.Micro:以编程方式创建和绑定视图

My main view has a DataGrid with the following XAML: 我的主视图有一个带有以下XAML的DataGrid:

<DataGrid Name="DataGridTestSuites" Grid.Row="1" 
      HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="12"
      IsReadOnly="True" AutoGenerateColumns="False"
      ItemsSource="{Binding TestSuites}"
      RowDetailsVisibilityMode="VisibleWhenSelected"
      cal:Message.Attach="[Event RowDetailsVisibilityChanged] = [Action PopulateTestSuiteDetail($this, $eventArgs)]">
<DataGrid.Columns>
    <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
    <DataGridTextColumn Header="Category" Binding="{Binding Category}" />
    <DataGridTextColumn Header="Assembly" Binding="{Binding AssemblyPath}" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <StackPanel x:Name="StackPanelTestSuiteDetail" />
    </DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>

Note the StackPanel in the DataTemplate. 请注意DataTemplate中的StackPanel。 This is where I want to inject my sub-view. 这是我要插入子视图的地方。

My view model has a function named PopulateTestSuiteDetails() which is attached to the RowDetailsVisibilityChanged event as described in the XAML: 我的视图模型具有一个名为PopulateTestSuiteDetails()的函数,该函数附加到XAML中所述的RowDetailsVisibilityChanged事件中:

public void PopulateTestSuiteDetail(DataModels.TestSuite testSuite, object eventArgs)
{
    if (!(eventArgs is DataGridRowDetailsEventArgs)) return;

    StackPanel stackPanel = (StackPanel)((DataGridRowDetailsEventArgs)eventArgs).DetailsElement.FindName("StackPanelTestSuiteDetail");

    var methodViewModel = IoC.Get<TestSuiteHelperMethodViewModel>();

    var methodView = new Harness.Views.TestSuiteHelperMethodView();

    stackPanel.Children.Add(methodView);
    ViewModelBinder.Bind(methodViewModel, methodView, null);
}

The function does get invoked correctly when I debug my program. 在调试程序时,该函数的确被正确调用。 However, it doesn't seem like my sub-view is being attached properly (my sub-view has a basic button and it is not visible when a data grid row is in focus.) Any idea why? 但是,好像我的子视图没有正确连接(我的子视图具有一个基本按钮,并且当焦点对准数据网格行时不可见。)知道为什么吗?

It turns out that I have to do the following to set my view and view model. 事实证明,我必须执行以下操作来设置我的视图和视图模型。

contentControl.Content = methodView;
Caliburn.Micro.View.SetModel(methodView, methodViewModel);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM