Data binding 仅在单击列后在comboboxcolumn中显示成员
我有一个控件,当我需要有两列的dislay person时: -全名 -好朋友 问题是,人身上的财产是一个对象。 一开始,这个人有他自己最好的朋友,但他可以从combobox列中更改它 现在,控件加载bestfriend后,列为空。 当我双击此列时,我可以更改bestfirend,它将设置此人的bestfriend 但我必须做什么才能在开始时不出现空白列 我认为,问题是,该控件无法将bestfriend与bestfriend集合进行匹配,因此我认为必须通过id进行匹配,但我不知道如何进行匹配Data binding 仅在单击列后在comboboxcolumn中显示成员,data-binding,silverlight-4.0,mvvm-light,telerik-grid,Data Binding,Silverlight 4.0,Mvvm Light,Telerik Grid,我有一个控件,当我需要有两列的dislay person时: -全名 -好朋友 问题是,人身上的财产是一个对象。 一开始,这个人有他自己最好的朋友,但他可以从combobox列中更改它 现在,控件加载bestfriend后,列为空。 当我双击此列时,我可以更改bestfirend,它将设置此人的bestfriend 但我必须做什么才能在开始时不出现空白列 我认为,问题是,该控件无法将bestfriend与bestfriend集合进行匹配,因此我认为必须通过id进行匹配,但我不知道如何进行匹配 &
<UserControl x:Class="MvvmLight1.MainPage"
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"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" mc:Ignorable="d"
Height="300"
Width="300"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<telerik:RadGridView x:Name="grdSrL"
AutoGenerateColumns="False"
SelectionMode="Single"
IsReadOnly="False"
IsFilteringAllowed="True"
Height="386"
Width="460"
HorizontalAlignment="Left"
CanUserDeleteRows="False"
CanUserInsertRows="True"
CanUserReorderColumns="False"
CanUserResizeColumns="True"
ItemsSource="{Binding Persons}">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn DataMemberBinding="{Binding FullName}" IsReadOnly="True" Header="FullName" />
<telerik:GridViewComboBoxColumn ItemsSource="{Binding Friends,Source={StaticResource Main}}" ItemsSourceBinding="{Binding Friends,Source={StaticResource Main}}" Header="1st"
DataMemberBinding="{Binding BestFriend}"
DisplayMemberPath="FullName" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>
</Grid>
</UserControl>
和视图模型:
using System.Collections.ObjectModel;
using GalaSoft.MvvmLight;
namespace MvvmLight1.ViewModel
{
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
for (int i = 0; i < 3; i++)
{
var friend = new Person() {FullName = "Name" + (i + 3).ToString()};
_friends.Add(friend);
_persons.Add(new Person(){FullName = "Name"+i.ToString(),Id = i,BestFirend = friend});
}
}
private ObservableCollection<Person> _persons=new ObservableCollection<Person>();
public ObservableCollection<Person> Persons
{
get { return _persons; }
set
{
_persons = value;
}
}
public ObservableCollection<Person> Friends
{
get { return _friends; }
set
{
_friends = value;
}
}
private ObservableCollection<Person> _friends=new ObservableCollection<Person>();
}
}
使用System.Collections.ObjectModel;
使用GalaSoft.MvvmLight;
名称空间MvvmLight1.ViewModel
{
公共类MainViewModel:ViewModelBase
{
公共主视图模型()
{
对于(int i=0;i<3;i++)
{
var friend=newperson(){FullName=“Name”+(i+3).ToString()};
_朋友。添加(朋友);
_添加(newperson(){FullName=“Name”+i.ToString(),Id=i,BestFirend=friend});
}
}
私人可观测集合_persons=新可观测集合();
公众人士
{
获取{return\u persons;}
设置
{
_人=价值;
}
}
公众观察收集之友
{
获取{return\u friends;}
设置
{
_朋友=价值;
}
}
private ObservableCollection_friends=新ObservableCollection();
}
}
和应用程序xaml
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MvvmLight1.App"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:MvvmLight1.ViewModel"
mc:Ignorable="d">
<Application.Resources>
<!--Global View Model Locator-->
<vm:ViewModelLocator x:Key="Locator"
d:IsDataSource="True" />
<vm:MainViewModel x:Key="Main"/>
</Application.Resources>
</Application>
不是
GridViewComboBoxColumn
方面的专家,但可能是它正在绑定列表中查找对象的实例,而该实例不在其中
使用“普通”组合框,您可以选择是使用值绑定还是项绑定。对于ItemBinding,组合框在值列表中查找相同的实例。如果找不到,则不会选择任何项目
如果是Valuebinding,则将SelectedValue与SelectedValuePath指定的值进行比较。这意味着列表条目和所选条目不需要是同一实例
但正如我所说,这是标准的组合框,就像Telerik控件一样。。。我真的不知道。但根据我使用它们(WebForm控件)的经验,如果你向它们的用户提问,它们是非常有用的
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MvvmLight1.App"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:MvvmLight1.ViewModel"
mc:Ignorable="d">
<Application.Resources>
<!--Global View Model Locator-->
<vm:ViewModelLocator x:Key="Locator"
d:IsDataSource="True" />
<vm:MainViewModel x:Key="Main"/>
</Application.Resources>
</Application>