【发布时间】:2013-05-15 10:01:54
【问题描述】:
我只是不知道我做错了什么。我正在尝试在 WPF 应用程序(4.5)上使用 Caliburn Micro。尝试遵循 MVVM 的价值。
我的虚拟机具有服务和授权属性。授权具有 SelectedService 的属性。我将控件命名为x:Name=Services,当我填充Services 属性时,它们会显示在RadGridView 中,但是当您在RadGridView 中选择一个项目时,它不会将SelectedItem 绑定回我的SelectedService 属性。是因为 Services 属性在一个级别,而 SelectedService 是更深的级别,Authorizations.SelectedService?
下面是我敢于发布的尽可能多的代码,而不会淹没帖子。希望这就足够了。
我觉得我已经非常接近“获得”Caliburn Micro 和 MVVM 了......
public class Authorization:BindableBase
{
public int ID
{
get { return this.id; }
set
{
this.id = value;
this.OnPropertyChanged();
}
}
public Service SelectedService
{
get { return this.selectedService; }
set
{
this.selectedService = value;
OnPropertyChanged();
}
}
public Member ActiveMember
{
get { return this.activeMember; }
set
{
this.activeMember = value;
this.OnPropertyChanged();
}
}
}
然后 CreateAuthViewModel 具有该模型以及用于填充可能的选项的属性,称为服务:
[Export(typeof(IScreen))]
public class CreateAuthViewModel : Screen, IHandle<MessageNotifier>
{
public Authorization Authorization
{
get { return this.authorization; }
set
{
this.authorization = value;
NotifyOfPropertyChange();
}
}
public BindableCollection<Service> Services
{
get { return services; }
set
{
services = value;
NotifyOfPropertyChange();
}
}
最后是我的视图,CreateAuthView:
<UserControl x:Name="CreateAuthUserControl"
x:Class="Green.Views.CreateAuthView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:cal="http://www.caliburnproject.org">
<telerik:RadExpander>
<StackPanel>
<telerik:RadGridView x:Name="Services"
IsReadOnly="True"
SelectionMode="Extended"
ScrollViewer.CanContentScroll="True" />
<telerik:RadDataPager x:Name="ServicesDataPager"
PageSize="10"
VerticalAlignment="Bottom"
Source="{Binding Items, ElementName=Services}" />
</StackPanel>
</telerik:RadExpander>
</UserControl>
【问题讨论】:
-
您认为 Authorization_SelectedService 命名控件在哪里?
-
视图中没有一个。我曾想过,对于 Combobox 和 Datagrid,约定是它将
x:Name绑定到 VM 上用于 itemsource 的匹配属性,并在 VM 上查找名为“Selected、Active 或 Single”的属性,然后与第一个属性相同的名称。这就是我试图让它去做的事情。它从不将 Datagrid 的 SelectedItem 设置为我的Authorization.SelectedItem属性。 -
什么数据网格?您有一个 RadGridView 和一个 RadDataPager。我在视图中看不到任何其他内容。
-
抱歉,口误,我的意思是 RadGridView。
标签: c# wpf data-binding mvvm caliburn.micro