代码背后有效,但MVVM没有

时间:2011-06-06 17:51:25

标签: silverlight-4.0 mvvm

我一直在处理在后面的代码中工作的东西,但是当我尝试绑定到MVVM时,没有任何东西显示出来。首先,我将展示代码,然后是MVVM(相同的xaml)。我想使用MVVM而不是代码。

Code Behind(works):

var loadOp = ctx.Load<GateBlox.Web.Models.Structure>(ctx.GetStructuresQuery());
        loadOp.Completed += (s, e) => { _treeView.ItemsSource = loadOp.Entities.Where(struc => !struc.StructureParentFK.HasValue); };

XAML

<Grid x:Name="LayoutRoot">
    <sdk:TreeView x:Name='_treeView' DataContext='{StaticResource ViewModel}'>
        <sdk:TreeView.ItemTemplate>
            <sdk:HierarchicalDataTemplate ItemsSource='{Binding Children}'>
                <TextBlock Text='{Binding StructureName}' />
            </sdk:HierarchicalDataTemplate>
        </sdk:TreeView.ItemTemplate>
    </sdk:TreeView>
</Grid>

MVVM(不绑定)

private LoadOperation<Structure> _loadStructures;
private StructureContext _structureContext;

private IEnumerable<Structure> _structures;
public IEnumerable<Structure> Structures
{
   get { return this._structures; }
   set { this._structures = value; RaisePropertyChanged("Structures"); }
}

public StructuresViewModel()
{
 if (!DesignerProperties.IsInDesignTool)
  {
      _structureContext = new StructureContext();

      _loadStructures = _structureContext.Load(_structureContext.GetStructuresQuery().Where (p=> !  p.StructureParentFK.HasValue));
  _loadStructures.Completed += new EventHandler(_loadStructures_Completed);
   }
}

void _loadStructures_Completed(object sender, EventArgs e)
{
 this.Structures = _loadStructures.Entities;
}

3 个答案:

答案 0 :(得分:1)

您是否检查过输出中没有出现绑定表达式错误?您将数据模板的项目源绑定到名为 Children 的属性,但您的视图模型公开了名为 Structures 的数据源。

此外,在您的工作示例中,您正在设置TreeView的ItemsSource,但在MVVM XAML中,您正在设置数据模板的ItemsSource。您需要设置/绑定的ItemsSource之间是否存在不一致?

您也可以考虑使用实现INotifyCollectionChanged界面(ObservableCollection)的集合数据源,或将绑定源公开为使用ICollectionViewPagedCollectionView。< / p>

我建议您查看有关data binding in MVVM的信息,因为它提供了有关在视图模型中设置数据源的出色指导。

答案 1 :(得分:0)

我现在差不多了。我采用了不同的方法并使用了HeirarchicalDataTemplate。目前数据显示但不正确:child1记录也作为父级记录。     Parent1(1级)     Parent2(1级)        Child1(级别2)     Child1(level1)

<navigation:Page x:Class="GateBlox.Views.Structure"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
             d:DesignWidth="640"
             d:DesignHeight="480"
             Title="Structure Page"
             xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
             xmlns:viewmodel="clr-namespace:GateBlox.ViewModels">

<UserControl.Resources>
    <viewmodel:StructuresViewModel x:Key='ViewModel'>
    </viewmodel:StructuresViewModel>
</UserControl.Resources>

<Grid x:Name="LayoutRoot"
      DataContext='{StaticResource ViewModel}'>
    <Grid.Resources>
        <sdk:HierarchicalDataTemplate x:Key="ChildTemplate"
                                      ItemsSource="{Binding Path=Parent}">
            <TextBlock FontStyle="Italic"
                       Text="{Binding Path=StructureName}" />
        </sdk:HierarchicalDataTemplate>
        <sdk:HierarchicalDataTemplate x:Key="NameTemplate"
                                      ItemsSource="{Binding Path=Children}"
                                      ItemTemplate="{StaticResource ChildTemplate}">
            <TextBlock Text="{Binding Path=StructureName}"
                       FontWeight="Bold" />
        </sdk:HierarchicalDataTemplate>
    </Grid.Resources>
    <sdk:TreeView x:Name='treeView'
                  Width='400'
                  Height='300'
                  ItemsSource='{Binding Structures}'
                  ItemTemplate='{StaticResource NameTemplate}'>
    </sdk:TreeView>
</Grid>

using System;
using System.Collections.ObjectModel;
using GateBlox.Web.Models;
using System.ServiceModel.DomainServices.Client;
using GateBlox.Web.Services;
using GateBlox.Helpers;
using System.ComponentModel;
using System.Collections.Generic;


namespace GateBlox.ViewModels
{
public class StructuresViewModel : ViewModelBase
{
    private LoadOperation<Structure> _loadStructures;
    private StructureContext _structureContext;


    private ObservableCollection<Structure> _structures;
    public ObservableCollection<Structure> Structures
    {
        get { return this._structures; }
        set { this._structures = value; RaisePropertyChanged("Structures"); }
    }

    public StructuresViewModel()
    {
        if (!DesignerProperties.IsInDesignTool)
        {
            _structureContext = new StructureContext();

            _loadStructures = _structureContext.Load(_structureContext.GetStructuresQuery());
            _loadStructures.Completed += new EventHandler(_loadStructures_Completed);
        }
    }

    void _loadStructures_Completed(object sender, EventArgs e)
    {
        this.Structures = IEnumerableConverter.ToObservableCollection(_loadStructures.Entities);
    }
}

}

答案 2 :(得分:0)

您没有为ItemsSource设置TreeView。我认为你的xaml应该是这样的:

<Grid x:Name="LayoutRoot">
  <sdk:TreeView x:Name='_treeView' DataContext='{StaticResource ViewModel}'
                ItemsSource="{Binding Structures}">
    <sdk:TreeView.ItemTemplate>
      <sdk:HierarchicalDataTemplate ItemsSource='{Binding Children}'>
         <TextBlock Text='{Binding StructureName}' />
      </sdk:HierarchicalDataTemplate>
    </sdk:TreeView.ItemTemplate>
  </sdk:TreeView>
</Grid>

希望这会有所帮助:)