views:

12

answers:

1

I am having major problem in Data Binding.

I have a stackpanel with an ItemControl in my MainPage.xml:

                <StackPanel>
                    <ItemsControl x:Name="TopicList">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <local:TopicListItem Title="{Binding Title}"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>

Then I hook a IEnumerable object on to that that contains an object with the property Title on it. It is done in the MainPage.xaml.cs (and I know that the LINQ part is working):

var resultStories = from story in resultXML.Descendants("story")
                    select new NewsStory {...};

Dispatcher.BeginInvoke(() => TopicList.ItemsSource = resultStories);

And inside my custom control TopicListItem I have created a DepenencyProperty and corresponding public property:

    #region Title (DependencyProperty)

    /// <summary> 
    /// Title
    /// </summary> 
    public String Title
    {
        get { return (String)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }
    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register("Title", typeof(String), typeof(TopicListItem),
        new PropertyMetadata(0, new PropertyChangedCallback(OnTitleChanged)));

    private static void OnTitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((TopicListItem)d).OnTitleChanged(e);
    }

    private void OnTitleChanged(DependencyPropertyChangedEventArgs e)
    {
        throw new NotImplementedException();
    }

    #endregion Title (DependencyProperty)

When I run this and it tries to set the ItemSource an error comes up on the Title property:

System.TypeInitializationException: The type initializer for 'NewsSync.TopicListItem threw an exception. ---> System.ArgumentException: Default value type does not match type of property.

--
As a side note: I have tried not declaring a DepenencyProperty for the Title property and just having it as a public String. But then I get conversion issues where it says that I cannot convert from System.[...].Binding to System.String

So I have really tried many things.

+1  A: 

This bit is your problem:-

 public static readonly DependencyProperty TitleProperty =
    DependencyProperty.Register("Title", typeof(String), typeof(TopicListItem),
    new PropertyMetadata(0, new PropertyChangedCallback(OnTitleChanged)));

Note the first parameter of the PropertyMetadata constructor is the default value of the dependency property. You have registered it as a typeof(String) but you are using an Int32 (0) as the initial value. Use null instead. You could also just use:-

public static readonly DependencyProperty TitleProperty =
    DependencyProperty.Register("Title", typeof(String), typeof(TopicListItem), null);

Since your code will throw an exception currently when a value is assigned to Title. You only need to specify a PropertyChangedCallback if you actually have something want to do when the property changes.

AnthonyWJones
This was hard one to crack and I did it by accident using a similar change: `public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(String), typeof(TopicListItem), new PropertyMetadata(null));`
WmasterJ
Thanks for the clarity I hadn't realized that part about the default value of the dependency property. Which by-the-way is something I have been looking for as well :D
WmasterJ