2011-06-06 95 views
1

我一直在搞東西,在後面的代碼工作,但當我嘗試綁定到MVVM,沒有顯示。首先,我將展示代碼,然後展示MVVM(同樣的xaml)。我想使用MVVM,而不是後面的代碼。作品背後的代碼,但MVVM不

代碼背後(的作品):

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; 
} 

回答

0

你是不是爲你TreeView設置ItemsSource。我想你應該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> 

希望這有助於:)

+0

感謝阿卜杜勒 - 你的代碼工作,雖然我必須說我不知道​​爲什麼。我的結構表是自引用的 - 這個XAML會綁定到孫子嗎? – Greg 2011-06-08 17:38:18

+0

你的代碼沒有工作,因爲你沒有指定你的'TreeView'的ItemsSource是什麼。如果你指定TreeView應該顯示「Structures」,那麼在你的代碼中沒有任何地方。我只是添加了('ItemsSource =「{Binding Structures}」')。 – AbdouMoumen 2011-06-08 17:52:42

+0

好的 - 但是如果你看看我的第二篇文章,我確實指定了樹視圖的ItemSource,結果是樹視圖沒有顯示子節點。 – Greg 2011-06-08 18:20:04

1

有你檢查,你沒有得到一個綁定表達式錯誤輸出?您正在將數據模板的項目源綁定到名爲子項的屬性,但您的視圖模型公開了名爲結構的數據源。

此外,在您的工作示例中,您正在設置TreeView的ItemsSource,但在您的MVVM XAML中,您正在設置數據模板的ItemsSource。您需要設置/綁定的ItemsSource之間是否存在不一致?

您也可以考慮使用實現INotifyCollectionChanged接口(ObservableCollection或將綁定源公開爲使用PagedCollectionViewICollectionView)的集合數據源。

我建議您查看關於data binding in MVVM的信息,因爲它提供了在視圖模型中設置數據源的絕佳指導。

+0

感謝您的答覆。 Structures模型來自具有自連接的數據庫表(如Northwind employees表)。我複製的示例使用後面的代碼並使用實體導航屬性的許多方面,並且工作正常,所以我在我的引用中留下了許多加入的一方(兒童)。在MVVM的例子中,我嘗試綁定到結構而不是孩子,但沒有奏效。我也嘗試使用Observable集合而不是IEnumerable,但沒有喜悅。 – Greg 2011-06-06 19:47:08

+0

另外,我已經檢查過數據肯定會回到視圖模型,所以問題必須在XAML中。 – Greg 2011-06-06 19:54:54

+0

你能告訴你如何設置你的靜態資源嗎?在這一點上,我想知道這個問題是否在你的DataContext中。你有沒有檢查調試輸出,以確認你沒有收到任何綁定錯誤?如果你在數據源的getter中得到了一個斷點,你打了嗎? (例如。XAML綁定正試圖檢索數據)。 – Oppositional 2011-06-06 19:56:59

0

我現在幾乎已經開始工作了。我採取了不同的方法,並採用了HeirarchicalDataTemplate。目前數據顯示但不正確:child1記錄也作爲父母縮減。 Parent1(1級) Parent2(1級) Child1(level2的) Child1(1級)

<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); 
    } 
} 

}