2011-11-12 75 views
2

我正在关注MVVM模式,并且我想使用XAML绑定ListView ItemsSource,而不是在即使 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> 

和菜单,该场所将落座于视图模型。选项是属性的类,所以我使用Menu.Option

我的菜单是ContentMenuModel类型的属性,而ContentMenuModel是包含Option和Title和Image属性的类。

看看哪些是视图模型

 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 
} 

我已经绑定的ViewModelLocator到我的主窗口的的DataContext和路径= ViewModel的MainMenu,因此MainMain是ViewModel的对象,我可以将此属性绑定到ListView的ItemsSource,但它不起作用。

请纠正我错在哪里。

回答

0

为了快速获得完整的示例,您可以安装NUGet for Visual Studio,并通过它在干净的WPF应用程序项目上安装MVVMLight包。然后它会把所有东西都放好,你会看到它是如何工作的。 虽然我会在这里描述基础知识。 MVVMLight支持开箱即用。 MVVMLigth的标准模板包括ViewModelLocatorMainViewModelViewModelLocator - 是保存所有其他视图模型的类。从一开始它只有一个物业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> 

在上面的例子中,我已经添加public string Text {get;}属性MainViewModel和参考。在示例中,不需要任何代码隐藏,everyting通过xaml声明式设置。

+0

嗨Vladimr我有同样的你解释我,但它不给我结果,因为我想要更早我有RadCarousel控制,它工作完美,但列表视图不工作。 –

+0

显示您的选项类和菜单类,它们的外观如何。 –

+0

我正在修改我的代码,只是看到它并纠正我的请求。谢谢。 –

0

只需添加您的ListView的Grid并将DataContext分配给源代码,它就可以工作。

相关问题