none
MVVMLight绑定了集合就是不显示,为什么 RRS feed

答案

  • 你好,

    根据有你的代码,你是在MainView中的ListBox中同时显示ABC三个ViewModel,这就意味着一个ListBox中要绑定三个不同的Model,这是不可能的。虽然你的页面显示出不同的A,B,C 三个ViewModel的条目信息,但是它们实际上是MainViewModel中ViewModelBase这一个Model,所以页面只显示出内容信息为MVVMLight.ViewModel.AViewModel等信息。

    我新建了一个MyViewModelBase类:

    MyViewModelBase.cs

     public class MyViewModelBase:ViewModelBase
        {
            private AView aViewMode;

            public AView AViewMode
            {
                get { return aViewMode; }
                set
                {
                    aViewMode = value;
                    RaisePropertyChanged(() => AViewMode);
                }
            }

            private BView bViewMode;

            public BView BViewMode
            {
                get { return bViewMode; }
                set
                {
                    bViewMode = value;
                    RaisePropertyChanged(() => BViewMode);
                }
            }

            private CView cViewMode;

            public CView CViewMode
            {
                get { return cViewMode; }
                set
                {
                    cViewMode = value;
                    RaisePropertyChanged(() =>CViewMode);
                }
            }
        }

    然后把你的MianViewModel中的ViewModelsCollection改为:

      MainModels.ViewModelsCollection = new ObservableCollection<ViewModelBase>()
                {
                    new MyViewModelBase(){ AViewMode=new AView(){ Content = "A" } },
                    new MyViewModelBase(){ AViewMode=new AView(){ Content = "As" } },
                    new MyViewModelBase(){ BViewMode=new BView(){ Content = "B" } },
                    new MyViewModelBase(){ BViewMode=new BView(){ Content = "Bs" } },
                    new MyViewModelBase(){ CViewMode=new CView(){ Content = "C" } },
                    new MyViewModelBase(){ CViewMode=new CView(){ Content = "Cs" } }             
                };

    MainView.xaml修改为:

     <ListBox ItemsSource="{Binding MainModels.ViewModelsCollection}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Background="Yellow">
                            <TextBlock Text="{Binding AViewMode.Content}" FontWeight="Bold" Foreground="Navy"/>
                            <TextBlock Text="{Binding BViewMode.Content}" FontWeight="Bold" Foreground="Navy"/>
                            <TextBlock Text="{Binding CViewMode.Content}" FontWeight="Bold" Foreground="Navy"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

    运行结果如图:

    谢谢

    Daisy Tian


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2020年8月24日 7:58