如何从用户控件的依赖属性中设置/获取值

本文关键字:设置 获取 属性 依赖 用户 控件 | 更新日期: 2023-09-27 18:17:06

我有一个UserControl,其中有一个网格,并显示一个人员列表,当单击我的网格中的行时,我在视图模型中选择了行项。我的问题是,我不能得到选定行在我的用户控制在我的view3model SelectedPersonModel .这里是我使用的代码:我的UserControl Xaml Code和Code Behind:

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

我想从我的视图模型设置用户控制PersonList,并得到selectedRowviewmodel SelectedPersonModel属性中的属性值。怎么做呢?

如何从用户控件的依赖属性中设置/获取值

只需将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的一部分

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