2011-11-12 15 views
2

MVVMパターンに従っていますが、ListView ItemsSourceをXAMLを使用してバインドしたい場合でも、 this.Datacontext = ObservableCollectionプロパティではバインドしません。コードビハインドにないXAMLを使用してListView ItemsSourceをバインドする方法。

私のコードは次のようである:

  <ListView x:Name="MenuBarList" 
        Grid.Row="2" 
        ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
        ItemsSource="{Binding Path=Menu.Option}" 
        Width="{Binding MainMenuWidth}" 
        SelectedItem="{Binding Path=SelectedMainMenuOption, Mode=TwoWay}" > 
      <ListView.ItemsPanel> 
       <ItemsPanelTemplate> 
        <WrapPanel Orientation="Vertical" /> 
       </ItemsPanelTemplate> 
      </ListView.ItemsPanel> 

      <ListView.ItemTemplate> 
       <DataTemplate> 
        <TextBlock Text="{Binding Title}" TextWrapping="Wrap" IsHitTestVisible="False" /> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 

とメニューが財産であり、それはViewModelにに着座します。オプションはプロパティのクラスなので、Menu.Optionを使用しています

マイメニューはContentMenuModel型のプロパティで、ContentMenuModelはOption、Title、Imageのプロパティを含むクラスです。

のViewModel

 public const string MenuPropertyName = "Menu"; 

    private ContentMenuModel _Menu = null; 

    /// <summary> 
    /// Gets the Menu collection. 
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary> 
    public ContentMenuModel Menu 
    { 
     get 
     { 
      return _Menu; 
     } 

     set 
     { 
      if (_Menu == value) 
       return; 

      _Menu = value; 

      // Update bindings, no broadcast 
      RaisePropertyChanged(MenuPropertyName); 
     } 
    } 

そしてContentMenuModelクラスの内部にあるメニューのプロパティを参照してくださいすることは次のようになります。

public class ContentMenuModel 
{ 

    #region Title 

    /// <summary> 
    /// The <see cref="Title" /> property's name. 
    /// </summary> 
    public const string TitlePropertyName = "Title"; 

    private string _Title = String.Empty; 

    /// <summary> 
    /// Gets the Title property. 
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary> 

    [Required] 
    [StringLength(128, ErrorMessage = "The Title value cannot exceed 128 characters. ")] 
    public string Title 
    { 
     get 
     { 
      return _Title; 
     } 

     set 
     { 
      if (_Title == value) 
      { 
       return; 
      } 

      var oldValue = _Title; 
      _Title = value; 

      // Update bindings, no broadcast 
      RaisePropertyChanged(TitlePropertyName); 
     } 
    } 

    #endregion 

    #region Options 

    /// <summary> 
    /// The <see cref="Options" /> property's name. 
    /// </summary> 
    public const string OptionsPropertyName = "Options"; 

    private ObservableCollection<ContentMenuOptionModel> _Options = null; 

    /// <summary> 
    /// Gets the Options property. 
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary> 

    public ObservableCollection<ContentMenuOptionModel> Options 
    { 
     get 
     { 
      return _Options; 
     } 

     set 
     { 
      if (_Options == value) 
      { 
       return; 
      } 

      var oldValue = _Options; 
      _Options = value; 

      RaisePropertyChanged(OptionsPropertyName); 
     } 
    } 

    #endregion 

    #region ContextText 

    /// <summary> 
    /// The <see cref="Options" /> property's name. 
    /// </summary> 
    public const string ContextTextPropertyName = "ContextText"; 

    private ContentPageItem _ContextText = null; 

    /// <summary> 
    /// Gets the ContextText property. 
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary> 

    public ContentPageItem ContextText 
    { 
     get 
     { 
      return _ContextText; 
     } 

     set 
     { 
      if (_ContextText == value) 
      { 
       return; 
      } 

      _ContextText = value; 

      RaisePropertyChanged(OptionsPropertyName); 
     } 
    } 

    #endregion 
} 

が、私は私のメインウィンドウのDataContextのとパスにViewModelLocatorをバインドさていました= ViewModelのMainMenu、MainMainは、このプロパティをListViewのItemsSourceにバインドできるViewModelのオブジェクトですが、機能しません。

私が間違っている場所を修正してください。

答えて

0

Visual Studio用のNUGetをインストールして、クリーンなWPFアプリケーションプロジェクトにMVVMLightパッケージをインストールすることで、すぐに例を得ることができます。そうすれば、すべてが設定され、どのように動作するかがわかります。 ここでは基本について説明しますが、 MVVMLightはこれをそのまま使用できます。 MVVMLigthの標準テンプレートには、ViewModelLocatorMainViewModelが含まれます。 ViewModelLocator - 他のすべてのビューモデルを保持するクラスです。最初から1つのプロパティーがありますpublic MainViewModel Main {get;}ViewModelLocatorが続いApp.xaml

<Application> 
    <Application.Resources> 
    <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" /> 
    </Application.Resources> 
</Application> 

内のリソースとして登録されているビューモデルに取得したい場合は、任意のページに、あなたは、単にロケータリソースを参照し、ページに適切なそのプロパティを取得する必要があります。ここでMainWindowとそのMainViewModelのための例です:上記の例で

<Window DataContext="{Binding Source={StaticResource Locator}, Path=Main}"> 
    <Grid> 
     <TextBlock Text="{Binding Text}"/> 
    </Grid> 
</Window> 

、私はMainViewModelpublic string Text {get;}プロパティを追加し、参照しました。たとえば、コードビハインドは必要ありません。すべてがxamlを介して宣言的にセットアップされます。

+0

こんにちはVladimr私はあなたが私に説明したが、それは私が以前にも私はRadCarouselコントロールを持っていたと私は結果を与えていないと完全に動作していたが、リストビューが動作していません。 –

+0

OptionクラスとMenuクラスを表示し、どのように表示されるかを示します。 –

+0

私のコードを修正しているだけで、私の嘆願を修正しています。ありがとうございました。 –

0

ListViewのグリッドを追加し、DataContextをソースに設定するだけで、それが機能します。

関連する問題