我检查了以下线程并按照示例尝试动态绑定视图: Caliburn.Micro: Create and Bind View programmatically
我的主视图有一个带有以下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>
注意DataTemplate中的StackPanel。这是我想要注入子视图的地方。
我的视图模型有一个名为PopulateTestSuiteDetails()的函数,它附加到RowDetailsVisibilityChanged事件,如XAML中所述:
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);
}
调试程序时,函数会被正确调用。但是,我的子视图似乎没有被正确附加(我的子视图有一个基本按钮,当数据网格行处于焦点时它不可见。)任何想法为什么?
答案 0 :(得分:1)
事实证明,我必须执行以下操作来设置视图和查看模型。
contentControl.Content = methodView;
Caliburn.Micro.View.SetModel(methodView, methodViewModel);