【问题标题】:How to Set/Get value from a Dependency Property of a usercontrol?如何从用户控件的依赖属性中设置/获取值?
【发布时间】:2011-12-08 15:40:58
【问题描述】:

我有一个用户控件,其中有一个网格并显示一个人员列表,当单击网格中的行时我消失了在我的视图模型中获取选定的行项目。我的问题是我无法在我的用户控件中获取选定的行在我的 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;}

我想从我的视图模型中设置 User Control PersonList 并获取 selectedRow viewmodel中的属性值SelectedPersonModel property.怎么做?

【问题讨论】:

    标签: c# wpf mvvm user-controls


    【解决方案1】:

    只需将您的SelectedRow 属性绑定到SelectedPersonViewModel

    此外,由于您的 ViewModel 中设置的 SelectedPersonModel 不存在于您的 PersonList 中,因此选择不会与您发布的代码一起出现。请参阅下面代码中的 cmets。

       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 的一部分。

    【讨论】:

    • 我更正了我的 UserControl 代码,我将 SelectedRow 绑定到 SelectedPersonModel :SelectedRow="{Binding Path=SelectedPersonModel ,Mode=TwoWay}" ,但是当我从 Datagrid 中选择一行时,我的 SelectedPersonModel 为 Null
    • @mtaboy 尝试将您的BindingList 切换为ObservableCollection。不确定这是否会有所作为,但这是我在您的代码中看到的唯一看起来不寻常的东西。我相信BindingList 的行为不同于ObservableCollection
    • 我从 ObservableCollection 使用,但我的问题没有解决。事实上,我想要一个用户控件,它显示来自诸如人员之类的项目的列表,并且可以从中获取选择的项目。我的实现是否错误?
    【解决方案2】:

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

    【讨论】:

    • 我必须在哪里设置 DataContext?
    • 我说的是“不要”而不是“在别处设置”。
    • @HB 虽然我同意你的观点,但你的回答并没有回答问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    相关资源
    最近更新 更多