简体   繁体   English

使用(Items?)控件通过MVVM管理UserControl的集合

[英]Using an (Items?)Control to manage a collection of UserControls with MVVM

Question: 题:

Is there a way to achieve what I'm looking for; 有没有一种方法可以实现我想要的? to simply add a collection of views (UserControls) to a control whilst having a consistent MVVM structure? 在具有一致的MVVM结构的同时,简单地将视图的集合(UserControls)添加到控件? Is an ItemsControl the way to go, or should be have a different approach? 是ItemsControl可以走的路,还是应该采用其他方法?

Problem 问题

I have a collection of MVVM projects (with usercontrol mainviews) that I want to present in another control that can animate transitions between them. 我有一组MVVM项目(带有usercontrol主视图),我想在另一个可以呈现它们之间过渡动画的控件中呈现。 I have a horizontal listbox across the top of the control functioning as 'tabs' which are bound to the collection of views. 我在控件顶部有一个水平列表框,用作“选项卡”,绑定到视图集合。 The main body of the control shows a single view bound to the selected item of the list box and animates transitions when a new tab is selected. 控件的主体显示绑定到列表框所选项目的单个视图,并在选择新选项卡时为过渡设置动画。

在此处输入图片说明

My concern is having the collection of views as part of the new control's viewmodel doesn't make much sense in the MVVM paradigm and it would be more usable if the usercontrols could simply by added straight into the control almost as if it was a panel. 我担心的是,将视图集合作为新控件的视图模型的一部分在MVVM范例中没有多大意义,如果用户控件可以简单地直接添加到控件中,就好像它是一个面板一样,它将更加有用。 This would lead me to think something like ItemsControl is the way to go and could be used like this: 这会让我认为像ItemsControl这样的方法是可行的,可以这样使用:

<CustomItemsControl>
    <UserControl1>
    <UserControl2>
    <UserControl3>
    <UserControl4>
</CustomItemsControl>

Then have content that looked something like this. 然后拥有看起来像这样的内容。

<ItemsControl.Template>

<StackPanel>
    <ListBox x:Name="Tabs" Height="35" SelectedIndex="0"
        ItemsSource= >> The binding for the ItemsControls Collection <<
        SelectionChanged="Tabs_TabSelected">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Rows="1" 
                             Columns= >> Binding to Collection.Count <<
                />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>

    <TransitionCtrl Name="TransitionControl" CurrentView="{Binding SelectedItem, ElementName=Tabs}" />

</StackPanel>

</ItemsControl.Template>

From the examples I've looked at the ItemsControl isn't intended to be used in this way. 从示例中,我看到了ItemsControl并非旨在以这种方式使用。 Any suggestions to point me in the direction of what I'm trying to achieve? 有什么建议可以指出我要达到的目标吗?

You should be using PRISM view injection technique. 您应该使用PRISM 视图注入技术。 The best approach would be using TabControl as a region and inject the main view when each module loaded. 最好的方法是使用TabControl作为区域,并在加载每个模块时注入主视图。 Since tab is not built as a region, you may need a custom region adapter as explained here . 由于片未建成一个区域,你可以作为解释的需要自定义区域适配器这里

<TabControl Grid.Row=”1″ Grid.Column=”1″
        cal:RegionManager.RegionName=”TabRegion” Name=”TabRegion”>

From every module, 在每个模块中

public void Initialize()
{
    regionManager
        .AddToRegion(“TabRegion”, new FirstView())
        .AddToRegion(“TabRegion”, new SecondView());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM