无法在 Caliburn.Micro 中以编程方式绑定视图
我检查了以下线程并按照示例尝试动态绑定视图: Caliburn.Micro:以编程方式创建和绑定视图
我的主视图有一个具有以下 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);
}
当我调试程序时,该函数确实被正确调用。但是,我的子视图似乎没有正确附加(我的子视图有一个基本按钮,当数据网格行处于焦点状态时它不可见。)知道为什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,我必须执行以下操作来设置我的视图和视图模型。
It turns out that I have to do the following to set my view and view model.