簡體   English   中英

Caliburn.Micro自定義ViewModelLocator:綁定ItemContainerStyle中的usercontrol屬性

[英]Caliburn.Micro custom ViewModelLocator: bind usercontrol property in ItemContainerStyle

我正在使用Xceed工具箱WindowContainer和ChildWindows生成由多個窗口(容器)組成的動態客戶區,每個窗口都包含多個自定義用戶控件(元素)。 這些窗口可以在WindowContainer區域中自由移動,調整大小並最大化。

為了表示ViewModel級別的層次結構,我使用Caliburn.Micros導體機制。 因此MainViewModel進行容器:

public class MainViewModel : Conductor<ElementContainerViewModel>.Collection.AllActive

容器執行以下元素:

public class ElementContainerViewModel : Conductor<ElementViewModel>.Collection.AllActive

相應的視圖如下所示(用於將默認的ContentPresenter替換為ChildWindow作為容器的自定義項目控件)。 MainView.xaml(為簡潔起見,省略了ElementContainerView.xaml):

<childWindowContainer:MyItemsControl x:Name="Items" >
    <childWindowContainer:MyItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <xctk:WindowContainer Height="800" Width="600" Background="LightGoldenrodYellow"/>
        </ItemsPanelTemplate>
    </childWindowContainer:MyItemsControl.ItemsPanel>
    <childWindowContainer:MyItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="xctk:ChildWindow.Left" Value="{Binding ContainerLeft}" />
            <Setter Property="xctk:ChildWindow.Top" Value="{Binding ContainerTop}" />
            <Setter Property="xctk:ChildWindow.Width" Value="{Binding ContainerWidth}" />
            <Setter Property="xctk:ChildWindow.Height" Value="{Binding ContainerHeight}" />
            <Setter Property="xctk:ChildWindow.CloseButtonVisibility" Value="Collapsed" />
            <Setter Property="xctk:ChildWindow.Content" Value="{Binding}" />
            <Setter Property="xctk:ChildWindow.IsMaximized" Value="{Binding IsMaximized, Mode=TwoWay}" />
        </Style>
    </childWindowContainer:MyItemsControl.ItemContainerStyle>
</childWindowContainer:MyItemsControl>

以下是引起我頭疼的那一行,摘自MainView項目控件:

<Setter Property="xctk:ChildWindow.Content" Value="{Binding}" />

綁定工作正常,但是相應的視圖未正確附加。 所以我只是添加了一個老式的數據模板來將容器ViewModel綁定到其相應的視圖:

<DataTemplate DataType="{x:Type viewModels:ElementContainerViewModel}">
   <views:ElementContainerView />
</DataTemplate>

問題:由於Caliburn.Micro不再執行ViewModel定位器過程,因此關閉應用程序會導致每個ChildWindow的nullreference異常,這是由Caliburn.Micro關閉進行的ViewModels而不是關閉ElementContainerView引起的。

我如何告訴Caliburn.Micro像在數據模板中一樣解決該約定。 當然,Views和ViewModels的組織方式正確,因此可以使用默認約定(RootNS.ViewModels.ElementContainerViewModel.cs / RootNS.Views.ElementContainerView.xaml)。 我擺弄了ViewLocator.NameTransformer和ConventionManager,但無法正常工作。

ConventionManager.AddElementConvention<ChildWindow>(ContentControl.ContentProperty, "Content",
        "DataContextChanged");
ViewLocator.NameTransformer.AddRule(@"^ElementContainerViewModel", @"ChildWindowContainer.Views.ElementContainerView");

非常感謝,Ly

您是否嘗試過使用cal:View.Model="{Binding}"來告訴caliburns視圖綁定引擎來查找模型的視圖,而不是使用DataTemplate?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM