如何将usercontrol放在wpf prism的itemsControl中

How to put usercontrol in itemsControl in wpf prism

我想使用 prism 将用户控件插入 wpf 中的 itemscontrol。 用户控件打印出来了,不知道怎么绑定数据

ViewA.xaml

<Grid>
        <ItemsControl ItemsSource="{Binding People}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ContentControl Grid.Row="1" Margin="10"
                        prism:RegionManager.RegionName="PersonDetailsRegion"
                        prism:RegionManager.RegionContext="{Binding}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>

ViewAViewModel.cs

private ObservableCollection<Person> _people;
        public ObservableCollection<Person> People
        {
            get { return _people; }
            set { SetProperty(ref _people, value); }
        }

        public ViewAViewModel()
        {
            CreatePeople();
        }


        private void CreatePeople()
        {
            var people = new ObservableCollection<Person>();
            for (int i = 0; i < 10; i++)
            {
                people.Add(new Person()
                {
                    FirstName = String.Format("First {0}", i),
                    LastName = String.Format("Last {0}", i),
                    Age = i
                });
            }

            People = people;
        }

将菜单项插入视图 A。

MenuItem.xaml

<Grid x:Name="LayoutRoot" Background="White">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- First Name -->
        <TextBlock Text="First Name:" Margin="5" />
        <TextBlock Grid.Column="1" Margin="5" Text="{Binding FirstName}" />

        <!-- Last Name -->
        <TextBlock Grid.Row="1" Text="Last Name:" Margin="5" />
        <TextBlock Grid.Row="1" Grid.Column="1"  Margin="5" Text="{Binding LastName}" />

        <!-- Age -->
        <TextBlock Grid.Row="2" Text="Age:" Margin="5"/>
        <TextBlock Grid.Row="2" Grid.Column="1"  Margin="5" Text="{Binding Age}"/>
    </Grid>
![现情况如下][1]
 https://i.stack.imgur.com/1VBai.png

如何进行数据绑定?请帮助我

谢谢你

这样不行。您必须在“外部”创建区域,即在 ItemsControl 上并删除 ItemsSource 绑定。然后多次导航到项目控制区域以填充它。

<ItemsControl prism:RegionManager.RegionName="PersonDetailsRegion"/>

或者,保留 ItemsSource 的绑定并删除里面的 ContentControl 并将 DataTemplate 放在某个地方供您查看。然后通过将视图模型放入项目源来填充它。

<DataTemplate DataType={x:Type Person}"><PersonView /></DataTemplate>
...    
<ItemsControl ItemsSource="{Binding People}"/>

就个人而言,大多数时候我更喜欢第二种变体,因为您可以避免将您个人的信息打包到导航参数中并取回它们的额外复杂性,更不用说更简单的方法了 PersonViewModel...