绑定从MVVM到View不在Prism中工作

时间:2012-03-08 21:32:30

标签: wpf mvvm prism prism-4

我是Prism的新手,但我已经成功构建了几个WPF / Mvvm-Light应用程序。我正在为每个View / ViewModel对使用ViewModel-first instaciation。应用程序打开时,视图全部被加载和停用。由于捕获针对他们的聚合事件,视图被激活。这是我尝试绑定到ViewModel中的数据的第一个视图。视图显示为预期,但我的列表框从未填充。只显示列表框的轮廓。如果我更改列表框的背景颜色,则会更改空列表框的颜色。 ViewModel属性有八行,但它们都不可见。我能够在列表框中显示硬编码的项目。我知道视图模型作为数据上下文加载到视图中,因为另一个文本块能够绑定到ViewModel属性它必须在我的列表框xaml中被破坏。以下是一些要审核的xaml:

  <UserControl
     x:Class="DxStudioSelect.View.DxStudioFindView"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
  >
     <UserControl.Resources>
        <DataTemplate x:Key="DxStudioListTemplate">
           <TextBlock Text="{Binding Path=FriendlyForkName}"/>
        </DataTemplate>
     </UserControl.Resources>
     <Grid>
        <Grid.ColumnDefinitions>
           <ColumnDefinition Width="*"/>
           <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <ListBox
           Grid.Column="0"
           ItemsSource="{Binding DatabaseInstanceList}"
           ItemTemplate="{StaticResource DxStudioListTemplate}"
        />
        <TextBlock Text="{Binding Path=PageName}" Grid.Column="1" FontSize="32" Foreground="Green" TextAlignment="Right"/>
     </Grid>
  </UserControl>

以下是代码隐藏:

  public partial class DxStudioFindView : UserControl, IDxStudioFindView {
     public DxStudioFindView() {
        InitializeComponent();
     }

     public IViewModel ViewModel {
        get { return (IDxStudioFindViewModel)DataContext; }
        set { DataContext = value; }
     }
  }

这是ViewModel:

  private readonly IEventAggregator _eventAggregator;
  private readonly IUnityContainer _unityContainer;
  private readonly IRegionManager _regionManager;
  private readonly string _dxStudioDatabaseName;
  private readonly HeaderUpdatePayload _headerUpdatePayload = new HeaderUpdatePayload("DxStudio", "Select DxStudio Instance");

  public DxStudioFindViewModel(IUnityContainer unityContainer, IRegionManager regionManager, IEventAggregator eventAggregator, IDxStudioFindView view)
     : base(view) {
     _unityContainer = unityContainer;
     _regionManager = regionManager;
     _eventAggregator = eventAggregator;
     View.ViewModel = this;

     if(IsInDesignMode) {

        //Design-time, so show fake data
        DesignTimeDataLoad();
     } else {

        //Run-time, so do the real stuff
        DesignTimeDataLoad();
        _dxStudioDatabaseName = LiteralString.DxStudioDatabaseNameTest;

        _eventAggregator.GetEvent<ViewChangeRequestEvent>().Subscribe(DxStudioInstanceChangeRequest, ThreadOption.UIThread, false, target => target.TargetView == LiteralString.DxStudioFind);
     }
  }


  public string PageName { get; set; }
  //public string PageName { get { return "Find DxStudio Instance"; } }

  private ObservableCollection<IDxStudioInstanceDto> _dxStudioInstanceList = null;
  public ObservableCollection<IDxStudioInstanceDto> DxStudioInstanceList {
     get { return _dxStudioInstanceList; }
     set {
        _dxStudioInstanceList = value;
        OnPropertyChanged("DxStudioInstanceList");
     }
  }

  private void DxStudioInstanceChangeRequest(ViewChangeRequestPayload payload) {
     var region = _regionManager.Regions[RegionNames.Content];
     region.Activate(View);

     _eventAggregator.GetEvent<ViewChangedHeaderEvent>().Publish(_headerUpdatePayload);

     var footerUpdatePayload = new FooterUpdatePayload(FooterDisplayMode.DxStudioSelect, _dxStudioDatabaseName, payload.TargetBackDatabase, payload.TargetBack, string.Empty, LiteralString.ToolboxStart);
     _eventAggregator.GetEvent<ViewChangedFooterEvent>().Publish(footerUpdatePayload);
  }

  private void DesignTimeDataLoad() {
     PageName = "Find DxStudio Instance";
     DxStudioInstanceList = new ObservableCollection<IDxStudioInstanceDto>() {
        new DxStudioInstanceDto("Instance1"),
        new DxStudioInstanceDto("Instance2"),
        new DxStudioInstanceDto("Instance3"),
        new DxStudioInstanceDto("Instance4"),
        new DxStudioInstanceDto("Instance5"),
        new DxStudioInstanceDto("Instance6"),
        new DxStudioInstanceDto("Instance7"),
        new DxStudioInstanceDto("Instance8"),
     };
  }

这是数据传输对象:

public class DxStudioInstanceDto : IDxStudioInstanceDto {
  public string FriendlyForkName { get; private set; }

  public DxStudioInstanceDto(string friendlyForkName) { FriendlyForkName = friendlyForkName; }
}

由于我完全没有想法,任何建议都会有所帮助。 感谢

1 个答案:

答案 0 :(得分:1)

您的列表与ItemsSource="{Binding DatabaseInstanceList}"绑定,但您的视图模型具有属性DxStudioInstanceList