Silverlight 4.0 代码隐藏可以工作,但MVVM不能
我一直在处理一些在代码背后工作的东西,但是当我尝试绑定到MVVM时,什么都没有显示。首先我将显示后面的代码,然后是MVVM(相同的xaml)。我想使用MVVM,而不是代码隐藏 代码隐藏(工程):Silverlight 4.0 代码隐藏可以工作,但MVVM不能,silverlight-4.0,mvvm,Silverlight 4.0,Mvvm,我一直在处理一些在代码背后工作的东西,但是当我尝试绑定到MVVM时,什么都没有显示。首先我将显示后面的代码,然后是MVVM(相同的xaml)。我想使用MVVM,而不是代码隐藏 代码隐藏(工程): var loadOp=ctx.Load(ctx.GetStructuresQuery()); loadOp.Completed+=(s,e)=>{u treeView.ItemsSource=loadOp.Entities.Where(struc=>!struct.StructureParentFK.H
var loadOp=ctx.Load(ctx.GetStructuresQuery());
loadOp.Completed+=(s,e)=>{u treeView.ItemsSource=loadOp.Entities.Where(struc=>!struct.StructureParentFK.HasValue);};
XAML
MVVM(不绑定)
private LoadOperation\u loadStructures;
私有结构上下文_StructureContext;
私有IEnumerable_结构;
公共可数结构
{
获取{返回这个。_;}
设置{this._structures=value;RaisePropertyChanged(“structures”);}
}
公共结构视图模型()
{
如果(!DesignerProperties.IsInDesignTool)
{
_structureContext=新建structureContext();
_loadStructures=\u structureContext.Load(\u structureContext.GetStructuresQuery().Where(p=>!p.StructureParentFK.HasValue));
_loadStructures.Completed+=新事件处理程序(_loadStructures_Completed);
}
}
void\u loadStructures\u已完成(对象发送方,事件参数e)
{
this.Structures=\u loadStructures.Entities;
}
是否已检查输出中是否没有出现绑定表达式错误?您正在将数据模板的项源绑定到名为Children的属性,但视图模型公开名为Structures的数据源
另外,在您的工作示例中,您正在设置TreeView的ItemsSource,但在MVVM XAML中,您正在设置数据模板的ItemsSource。您需要设置/绑定到的ItemsSource之间是否存在不一致
您也可以考虑使用实现接口的集合数据源(或者将绑定源公开为使用A)。
我建议您查看有关的这些信息,因为它为在视图模型中设置数据源提供了极好的指导。是否检查了输出中是否没有出现绑定表达式错误?您正在将数据模板的项源绑定到名为Children的属性,但视图模型公开名为Structures的数据源 另外,在您的工作示例中,您正在设置TreeView的ItemsSource,但在MVVM XAML中,您正在设置数据模板的ItemsSource。您需要设置/绑定到的ItemsSource之间是否存在不一致
您也可以考虑使用实现接口的集合数据源(或者将绑定源公开为使用A)。
我建议您查看关于的这些信息,因为它为在视图模型中设置数据源提供了极好的指导。我现在几乎可以使用它了。我采用了不同的方法,使用了一个HierarchicalDataTemplate。目前,数据正在显示,但显示不正确:child1记录也显示为父记录。 家长1(一级) 家长2(一级) 儿童1(二级) 儿童1(1级)
使用系统;
使用System.Collections.ObjectModel;
使用GateBlox.Web.Models;
使用System.ServiceModel.DomainServices.Client;
使用GateBlox.Web.Services;
使用GateBlox.Helpers;
使用系统组件模型;
使用System.Collections.Generic;
命名空间GateBlox.ViewModels
{
公共类结构ViewModel:ViewModelBase
{
私有加载操作_加载结构;
私有结构上下文_StructureContext;
私有可观测集合结构;
公共收集结构
{
获取{返回这个。_;}
设置{this._structures=value;RaisePropertyChanged(“structures”);}
}
公共结构视图模型()
{
如果(!DesignerProperties.IsInDesignTool)
{
_structureContext=新建structureContext();
_loadStructures=\u structureContext.Load(\u structureContext.GetStructuresQuery());
_loadStructures.Completed+=新事件处理程序(_loadStructures_Completed);
}
}
void\u loadStructures\u已完成(对象发送方,事件参数e)
{
this.Structures=IEnumerableConverter.ToObservableCollection(_loadStructures.Entities);
}
}
}我现在几乎可以用了。我采用了不同的方法,使用了一个HierarchicalDataTemplate。目前,数据正在显示,但显示不正确:child1记录也显示为父记录。 家长1(一级) 家长2(一级) 儿童1(二级) 儿童1(1级)
使用系统;
使用System.Collections.ObjectModel;
使用GateBlox.Web.Models;
使用System.ServiceModel.DomainServices.Client;
使用GateBlox.Web.Services;
使用GateBlox.Helpers;
使用系统组件模型;
使用System.Collections.Generic;
命名空间GateBlox.ViewModels
{
公共类结构ViewModel:ViewModelBase
{
私有加载操作_加载结构;
私有结构上下文_StructureContext;
私有可观测集合结构;
公共收集结构
{
获取{返回这个。_;}
设置{this._structures=value;RaisePropertyChanged(“structures”);}
}
公共结构视图模型()
{
如果(!DesignerProperties.IsInDesignTool)
{
_structureContext=新建structureContext();
_loadStructures=\u structureContext.Load(\u structureContext.GetStructuresQuery());
_loadStructures.Completed+=新事件处理程序(_loadStructures_Completed);
}
}
void\u loadStructures\u已完成(对象发送方,事件参数e)
{
this.Structures=IEnumerableConv
var loadOp = ctx.Load<GateBlox.Web.Models.Structure>(ctx.GetStructuresQuery());
loadOp.Completed += (s, e) => { _treeView.ItemsSource = loadOp.Entities.Where(struc => !struc.StructureParentFK.HasValue); };
<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>
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;
}
<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);
}
}
<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>