I checked out the following thread and followed the example to try and bind a view dynamically: Caliburn.Micro: Create and Bind View programmatically
My main view has a DataGrid with the following XAML:
<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. 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:
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.