2011-09-04 43 views
0

我有一个控制,当我需要dislay人有两个栏后显示在comboboxcolumn成员: -fullname -best朋友只有点击列

的问题是,在人这个属性最好的朋友是一个对象。 在开始人有他自己的最好的朋友,但他可以改变它从组合框列。

现在,控件加载后最好的列是空白的。 当我双击这个专栏时,我可以改变bestfirend,并设置此人的最好朋友。

但是我必须做些什么才能在开始不空栏?

我想,问题是,控制不能匹配最好的朋友,收集最好的朋友,所以我认为我必须通过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> 

主要型号:

namespace MvvmLight1 
{ 
    public class Person:INotifyPropertyChanged 
    { 
     private string _fullName; 

     public string FullName 
     { 
      get { return _fullName; } 
      set 
      { 
       if (_fullName!=value) 
       { 
        _fullName = value; 
        OnPropertyChanged("FullName"); 
       } 
      } 
     } 

     public int Id 
     { 
      get { return _id; } 
      set { _id = value; } 
     } 

     public Person BestFirend 
     { 
      get { return _bestFirend; } 
      set 
      { 
       if (_bestFirend!=value) 
       { 
        _bestFirend = value; 
        OnPropertyChanged("BestFirend"); 
       } 
      } 
     } 

     private int _id; 

     private Person _bestFirend; 

     public event PropertyChangedEventHandler PropertyChanged; 

     protected virtual void OnPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 

      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 
} 

和视图模型:

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

    } 
} 

和应用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> 

回答

0

不上GridViewComboBoxColumn的专家,但会不会是它正在寻找一个实例对象在绑定列表中,并且该实例不在其中?

对于“普通”组合框,您可以选择是否使用值绑定或项目绑定。在itembindng的情况下,ComboBox将在值列表中查找相同的实例。如果它找不到它,它不会选择任何项目。

如果使用Valuebinding,则将SelectedValue与SelectedValuePath指定的值进行比较。这意味着没有要求列表条目和所选条目是相同的实例。

但正如我所说,这是对于框标准ComboBoxes,至于Telerik控件......我真的不知道。但是根据我的经验(使用WebForm控件),如果您在其用户support forums中提出问题,那么这些问题很有帮助。