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

发布于 2024-12-13 00:11:50 字数 1921 浏览 2 评论 0原文

我检查了以下线程并按照示例尝试动态绑定视图: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假装爱人 2024-12-20 00:11:50

事实证明,我必须执行以下操作来设置我的视图和视图模型。

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文