我有一个使用MVVM和Prism的Silverlight项目。
我关注http://www.telerik.com/help/silverlight/patterns-and-practices-eventtocommand-prism.html
我在网格中选择一行,调试器点击我设置Detail对象的预期方法(SelectPerson)。但是我认为详细对象(PersonDetail)的绑定表达式是错误的。
这是我的ViewModel:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
namespace RadControlsSilverlight
{
public class PersonViewModel
{
public ObservableCollection<PersonInfo> GridItems { get; set; }
public PersonInfo PersonDetail { get; set; }
public DelegateCommand SelectPersonCommand { get; set; }
public PersonViewModel()
{
SetupData();
SelectPersonCommand = new DelegateCommand(SelectPerson);
}
public void SelectPerson(object obj)
{
Telerik.Windows.Controls.SelectionChangeEventArgs e = obj as Telerik.Windows.Controls.SelectionChangeEventArgs;
PersonInfo person = (PersonInfo)e.AddedItems[0];
PersonDetail = person;
}
public void SetupData()
{
Random rnd = new Random();
GridItems = new ObservableCollection<PersonInfo>();
for (int i = 0; i < 100; i++)
{
PersonInfo edi = new PersonInfo();
edi.ID = i;
edi.Name = "Name " + i.ToString();
edi.Date = DateTime.Today.AddDays(i);
edi.IsAvailable = (i % 3 == 0 ? true : false);
GridItems.Add(edi);
}
}
}
}
这是视图
<UserControl x:Class="RadControlsSilverlight.PersonList"
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:prismcommands="clr-namespace:RadControlsSilverlight.PrismCommands"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:local="clr-namespace:RadControlsSilverlight"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<local:PersonViewModel x:Key="xVM" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot"
Background="White"
DataContext="{StaticResource xVM}">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<telerik:RadGridView x:Name="xRadGridView"
ItemsSource="{Binding GridItems, Mode=TwoWay}"
prismcommands:SelectionChangedCommand.Command="{Binding SelectPersonCommand}"
>
</telerik:RadGridView>
<telerik:RadDataForm x:Name="DataForm1"
Grid.Column="1"
CurrentItem="{Binding PersonDetail, Mode=TwoWay}"
Header="Person Detail"/>
</Grid>
</UserControl>
我需要更改什么才能在DataForm中显示已选中的单个PersonInfo对象?
答案 0 :(得分:1)
您缺少事件以通知UI PersonDetail要更新然后重新绑定。您可以通过在PRISM中实现NotificationObject
来实现此目的。
public class PersonViewModel : NotificationObject
{
private PersonInfo _personDetail;
public PersonInfo PersonDetail
{
get { return _personDetail; }
set
{
if(_personDetail != value)
{
_personDetail = value;
//Notify UI of update
RaisePropertyChanged(() => PersonDetail);
}
}
}
}