2012-03-08 4 views
1

私はプリズムを初めて使用しましたが、いくつかのWPF/Mvvm-Lightアプリケーションを作成しました。私はそれぞれのView/ViewModelペアに対してViewModel-first instaciationを使用しています。アプリケーションが開くと、ビューはすべてロードされ、非アクティブになります。ビューは、それらを対象とする集計イベントをキャッチした結果としてアクティブ化されます。これは私がViewModelのデータにバインドしようとした最初のビューです。ビューは、リストボックスが決して表示されないことを除いて、期待通りに表示されます。リストボックスのアウトラインのみが表示されます。リストボックスの背景色を変更すると、空のリストボックスの色が変更されます。 ViewModelプロパティには8つの行がありますが、いずれも表示されません。ハードコードされた項目をリストボックスに表示することができます。別のテキストブロックがViewModelプロパティにバインドできるので、ビューモデルがデータコンテキストとしてビューにロードされていることがわかります。これはリストボックスxamlで壊れているものでなければなりません。ここで検討するいくつかのXAMLさ:MVVMからビューへのバインドがプリズムで機能しない

<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

リストはItemsSource="{Binding DatabaseInstanceList}"にバインドされていますが、ビューモデルのプロパティはDxStudioInstanceListです。

+0

Thwuuupppp ... [手の額に手を打つ音。]私にそれを見つけてくれてありがとう。時々、森は木々を見ている。 – JimBoone

関連する問題