如何从usercontrol的依赖属性设置/获取值?

时间:2011-12-08 15:40:59

标签: c# wpf mvvm user-controls

我有一个UserControl,其中有一个网格,并显示一个人的列表,当我在我的视图模型中点击我的网格中的行获取选择的行项目时我会减少。我的问题是我无法在我的usercontrol中选择行在我的view3model SelectedPersonModel。这是我正在使用的代码: 我的UserControl Xaml代码和代码背后:

<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}"  />

我的ViewModel:

    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;}

我希望从我的viewmodel设置User Control PersonList并获取selectedRow viewmodel中的属性值SelectedPersonModel property.how可以吗?

2 个答案:

答案 0 :(得分:1)

只需将SelectedRow媒体资源绑定到SelectedPersonViewModel

即可

此外,由于您的SelectedPersonModel中不存在ViewModel中设置的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的一部分。

答案 1 :(得分:0)

不要在UC上设置DataContext,它会影响你的“外部”绑定。