【问题标题】:WPF C# Custom UserControl How To add a Property which accepts an BindingWPF C#自定义用户控件如何添加一个接受绑定的属性
【发布时间】:2011-01-19 21:07:26
【问题描述】:

我正在尝试设计一个由 TextEditor 和 PopUp 组成的 CustomUserControl...

所以 Popup 控件应该绑定到一个列表... 我称它为BindingList。此属性应接受任何类型,例如 ObservableCollection、List、Ienumerable,例如(Collections)...

<my:CustomControl BindingList="{Binding Path=Collection}"


 public IEnumerable<object> BindingList
    {
        get { return (IEnumerable<object>)GetValue(BindingListProp); }
        set { SetValue(BindingListProp, value); }
    }

BindinglistProp

 public static readonly DependencyProperty BindingListProp = DependencyProperty.Register(??????

我不知道它应该如何接受绑定。

我应该如何处理通过的集合?当它是我不知道的类型时

喜欢

    class Person
    {
        private string _Name;
        private string _forename;


        public string Name
        {
            get { return _Name; }
            set
            {
                _Name = value;
            }
        }

        public string Forename
        {
            get { return _forename; }
            set
            {
                _forename = value;
            }
        }
    }

感谢任何提示、教程或代码 sn-ps。

真诚的 标记

【问题讨论】:

    标签: c# wpf wpf-controls custom-controls dependency-properties


    【解决方案1】:
    public IObservable<object> BindingList
    {
      get { return (IObservable<object>)base.GetValue(BindingListProperty); }
      set { base.SetValue(BindingListProperty, value); }
    }
    
    public static DependencyProperty BindingListProperty =
      DependencyProperty.Register(
      "BindingList",
      typeof(IObservable<object>),
      typeof(CustomControl),
      new PropertyMetadata(null));
    

    【讨论】:

    • 也许是 IObservable 而不是 IEnumerable?
    • 如何访问我在 CustomControl 中绑定的内容?当我将它与 ObservableCollection 绑定时,该属性保持为空。我很抱歉所有的问题......
    【解决方案2】:

    查看CollectionViewSource.GetDefaultView 以以通用方式处理任何集合。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-12
      • 2018-04-09
      • 1970-01-01
      • 2014-08-11
      • 2015-06-03
      • 2013-08-09
      • 1970-01-01
      相关资源
      最近更新 更多