jinfox
Topic Author
Posts: 11
Joined: 06 Apr 2019, 00:11

Freezable Collection issue (appear null) as dependency property xaml

29 Mar 2021, 12:30

Hello,

We have created a Freezable collection of framework element to be able to create static collection as dependencyProperties like so :
C#:

public class ParameterCollection : FreezableCollection<ParameterViewData> { }
public class ParameterViewData : FrameworkElement { ...  }

public class TextContentControl: ContentControl
{
   public ParameterCollection TextParameters
        {
            get { return (ParameterCollection)GetValue(TextParametersProperty); }
            set { SetValue(TextParametersProperty, value); }
        }

        public static readonly DependencyProperty TextParametersProperty = DependencyProperty.Register(nameof(TextParameters), typeof(ParameterCollection), typeof(TextContentControl),
                new PropertyMetadata(null, OnTextParametersChanged));
}
usage in xaml:
 <common:TextContentControl >
  	<common:TextContentControl.TextParameters>
             <common:ParameterViewData Key="Foo" Value="{Binding Holding.Tier}"/>
        </common:TextContentControl.TextParameters>
 </common:TextContentControl>
However, Blend doesn't like this piece of code :
Error:Collection property 'TextContentControl'.'TextParameters' is null.
Is there anything I have forgot to do in order to be able to initialize this type in xaml?

Thanks for all your work!
 
User avatar
sfernandez
Site Admin
Posts: 2109
Joined: 22 Dec 2011, 19:20

Re: Freezable Collection issue (appear null) as dependency property xaml

30 Mar 2021, 12:36

Hi, you need to set the property when the control is constructed:
public class TextContentControl: ContentControl
{
  public TextContentControl()
  {
    TextParameters = new ParameterCollection();
  }
  ...
}
 
jinfox
Topic Author
Posts: 11
Joined: 06 Apr 2019, 00:11

Re: Freezable Collection issue (appear null) as dependency property xaml

30 Mar 2021, 17:58

It worked. Thank you!
 
User avatar
sfernandez
Site Admin
Posts: 2109
Joined: 22 Dec 2011, 19:20

Re: Freezable Collection issue (appear null) as dependency property xaml

30 Mar 2021, 19:30

Great, thanks for the update.

Who is online

Users browsing this forum: No registered users and 3 guests