开发者

WPF - Expose a Bindable ReadOnly Dependency Property and add internally values

开发者 https://www.devze.com 2023-02-03 22:28 出处:网络
Thats my code: public static readonly DependencyPropertyKey ItemsSourceKey = DependencyProperty.RegisterReadOnly(\"ItemsSource\", typeof(List<string>), typeof(MyTextBox), null);

Thats my code:

 public static readonly DependencyPropertyKey ItemsSourceKey =
            DependencyProperty.RegisterReadOnly("ItemsSource", typeof(List<string>), typeof(MyTextBox), null);


        public static readonly DependencyProperty ItemsSourceProperty = ItemsSourceKey.DependencyProperty;
        public List<string> ItemsSource
        {
            get { return (List<string>)GetValue(ItemsSourceProperty); }
        }

I have 2 problems here:

1.) Since I made it a DependencyPropertyKey as suggested on MSDN I can not see anymore the ItemsSource in my XAML.

2.) The user should be able to bind to the List getting the current strin开发者_C百科gs in the ItemsSource of MyTextBox control. Internally inside MyTextBox I want to add strings to the ItemsSource but I can not create an instance of a List and assign it to ItemsSource as it is ReadOnly...

How can I solve that? I want a bindable readonly Property to which I can set data internally. Maybe you ask why I do not use the .Text Property to bind there. Well the user enters Data, I change it and want to return it changed in a list...


This problem is addressed in ItemsControl by using two separate properties: ItemsSource for Binding, Items as the readonly collection object used for actual display of data. Since ItemsControl already handles connecting these collections for you and doing all the necessary updates, if you want this behavior you should derive from ItemsControl. If you need the behavior of both ItemsControl and TextBox you can either create a control that is a composite of both and passes through properties to the internal controls, or create two related derived controls (one ItemsControl, one TextBox) that work with each other.


How can I solve that? I want a bindable readonly Property to which I can set data internally. Maybe you ask why I do not use the .Text Property to bind there. Well the user enters Data, I change it and want to return it changed in a list...

If you just need to modify your data you can use a converter for that.

0

精彩评论

暂无评论...
验证码 换一张
取 消