WPF Dependency Property Property and Link Type Default Values

If I create a custom control as follows:

public class MyControl : ContentControl
{
   public static readonly DependencyProperty ItemsProperty =               
         DependencyProperty.Register(
                "Items", 
                typeof(ObservableCollection<object>), 
                typeof(MyControl), 
                new PropertyMetadata(null));

   public MyControl()
   {   
       // Setup a default value to empty collection
       // so users of MyControl can call MyControl.Items.Add()
       Items = new ObservableCollection<object>();
   }

   public ObservableCollection<object> Items
   { 
      get { return (ObservableCollection<object>)GetValue(ItemsProperty); } 
      set { SetValue(ItemsProperty, value); } 
   } 
}

And then allow the user to contact him in Xaml as follows:

<DataTemplate>
    <MyControl Items="{Binding ItemsOnViewModel}"/>
</DataTemplate>

Then the binding never works! This is due to the Dependency Property Priority , which sets the CLR Set values ​​above the template bindings!

So, I understand why this is not working, but I wonder if there is a solution. Is it possible to provide a default ItemProperty value for the new ObservableCollection for lazy MyControl consumers who just want to add items programmatically, allowing MVVM Power Control My Control users to communicate with the same property through a DataTemplate?

Silverlight WPF. DynamicResource , Silverlight: (

:

, SetCurrentValue(ItemsProperty, new ObservableCollection<object>()); , - WPF. , . - Silverlight? , !: S

:

-, SetCurrentValue .NET3.5 , Silverlight . , () .

SetCurrentValue .NET3.5 Value Coercion
Value Coercion Silverlight

+1
2

ObservableCollection , . , , , -. ObservableCollection ( ). DependencyProperty, . INotifyPropertyChanged. , :

: . , GetValue, . .

public ObservableCollection<object> Items
{ 
    get
    {
        ObservableCollection<object> coll = (ObservableCollection<object>)GetValue(ItemsProperty);
        if (coll == null)
        {
            coll = new ObservableCollection<object>();
            this.SetValue(ItemsProperty, coll);
        }

        return coll;
    }
    set 
    {
        ObservableCollection<object> coll = Items;
        coll.Clear();
        foreach(var item in value)
            coll.Add(item);
    }
} 

, . , ItemProperty ObservableCollection (.. PropertyMetadata ( ObservableCollection()). . , , . , INotifyPropertyChanged, ...

0

:

  public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register(
        "Items",
        typeof(ObservableCollection<object>),
        typeof(CaseDetailControl),
        new PropertyMetadata(new ObservableCollection<object>()));

, ?

Edit:

ah... , getter?:

    public ObservableCollection<object> Items
    {
        get
        {
            if ((ObservableCollection<object>)GetValue(ItemsProperty) == null)
            {
                this.SetValue(ItemsProperty, new ObservableCollection<object>());
            }

            return (ObservableCollection<object>)GetValue(ItemsProperty);
        }

        set
        {
            this.SetValue(ItemsProperty, value);
        }
    }
0

Source: https://habr.com/ru/post/1776008/


All Articles