2011-12-08 129 views
2

我有一個用戶控件,它有一個網格,並顯示一個人的列表,當我點擊在我的網格中的行時,我得到了獲取選定的行項目在我的視圖model.my問題是,我不能得到選定的行在我的usercontrol中我view3model SelectedPersonModel。在這裏的,我正在使用的代碼: 我的用戶XAML代碼和代碼背後:如何設置/從usercontrol的依賴項屬性獲取值?

<Grid> 
    <DataGrid ItemsSource="{Binding PersonList,Mode=TwoWay,RelativeSource={RelativeSource AncestorType={x:Type Control:UcPersonList}}}" SelectedItem="{Binding SelectedRow,RelativeSource={RelativeSource AncestorType={x:Type Control:UcPersonList}}}"/> 
</Grid> 
public partial class UcPersonList : UserControl 
    { 
     public UcPersonList() 
     { 
      InitializeComponent(); 
      this.DataContext = this; 
     } 

    #region PersonListProperty 


    public static readonly DependencyProperty PersonListProperty = 
    DependencyProperty.Register("PersonList", typeof(BindingList<PersonModel>), typeof(UcPersonList), 
     new FrameworkPropertyMetadata 
     { 
      DefaultValue = new BindingList<PersonModel>(), 
      BindsTwoWayByDefault = true 
     }); 

    public BindingList<PersonModel> PersonList 
    { 
     get { return (BindingList<PersonModel>)GetValue(PersonListProperty); } 
     set 
     { 
      SetValue(PersonListProperty, value); 

     } 
    } 

    #endregion 

    #region SelectedPerson 



    public static readonly DependencyProperty SelectedRowProperty = 
    DependencyProperty.Register("SelectedRow", typeof(PersonModel), typeof(UcPersonList), 
     new FrameworkPropertyMetadata 
     { 
      DefaultValue = new PersonModel(), 
      BindsTwoWayByDefault = true 

     }); 

    public PersonModel SelectedRow 
    { 
     get { return (PersonModel)GetValue(SelectedRowProperty); } 
     set 
     { 
      SetValue(SelectedRowProperty , value); 

     } 
    } 

    #endregion 
} 

在我看來,我有:

<my:UcPersonList x:Name="uclist" Grid.Row="2" PersonList="{Binding Path=PersonList,Mode=TwoWay}" SelectedRow="{Binding Path=SelectedPersonModel ,Mode=TwoWay}" /> 

我的視圖模型:

public MainViewModel() 
    { 

     SelectedPersonModel = new PersonModel(); 
     PersonList = new BindingList<PersonModel>(); 
     PersonList.Add(new PersonModel { FirstName = "A", LastName = "AA", Age = 19 }); 
     PersonList.Add(new PersonModel { FirstName = "B", LastName = "BB", Age = 25 }); 
     PersonList.Add(new PersonModel { FirstName = "C", LastName = "CC", Age = 30 }); 
    } 
    public BindingList<PersonModel> PersonList { get; set; } 
    public PersonModel SelectedPersonModel{get;set;} 

我想那將用戶控制PersonList從我的視圖模型和視圖模型中SelectedPersonModel property.how得到selectedRow 屬性值做呢?

回答

0

不要在UC上設置DataContext,它會影響你的「外部」綁定。

+0

我必須在哪裏設置DataContext? –

+0

我說「不要」不是「把它放在別處」。 –

+2

@HB雖然我同意你的看法,但你的回答並沒有回答 – Rachel

1

SelectedRow屬性只是綁定到SelectedPersonViewModel

另外,選擇不會跟你發佈的代碼出現,因爲SelectedPersonModel集的視圖模型並不在你的PersonList存在。請參閱下面的代碼。

public MainViewModel() 
    { 
     SelectedPersonModel = new PersonModel(); 
     PersonList = new BindingList<PersonModel>(); 
     PersonList.Add(new PersonModel { FirstName = "A", LastName = "AA", Age = 19 }); 
     PersonList.Add(new PersonModel { FirstName = "B", LastName = "BB", Age = 25 }); 
     PersonList.Add(new PersonModel { FirstName = "C", LastName = "CC", Age = 30 }); 

     // Either add SelectedPerson to list 
     PersonList.Add(SelectedPersonModel); 

     // or set SelectedPersonModel to an item that already exists in the list 
     SelectedPersonModel = PersonList.FirstOrDefault(); 
    } 

另外我同意HB,不要在你的UserControl中設置你的DataContext。它應該在使用UserControl時設置,而不是作爲UserControl的一部分。

+0

這個問題我修正了我的UserControl Code,我將SelectedRow選擇到SelectedPersonModel:SelectedRow =「{Binding Path = SelectedPersonModel,Mode = TwoWay}」,但是當我選擇一行從Datagrid我的SelectedPersonModel是NULL –

+0

@mtaboy嘗試切換您的'BindingList'到'ObservableCollection'。不知道這是否會有所作爲,但這是我在代碼中看到的唯一不尋常的東西。我相信'BindingList'行爲不同於'ObservableCollection' – Rachel

+0

我使用從ObservableCollection但我的可擴展性不解決。事實上,我想要一個用戶控件,顯示一個項目,如人的列表,並可以從它獲得selcected項目。我的實現這是錯的嗎? –

相關問題