Let me explain my situation. I have made a user control that contains an ItemsControl
<ItemsControl Name="itemControlReviewTags">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<my:ReviewControl ReviewEvent="{Binding}" />
<TextBlock Text="{Binding Text}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
This ItemsControl is bound to an observablecollection in the code behind
public ObservableCollection<TagEvent> tagItems = new ObservableCollection<TagEvent>();
The collection is set on the ItemsControl like so
itemControlReviewTags.ItemsSource = tagItems;
The TagEvent class is defined like below. The class is added to the collection at certain events.
public class TagEvent : EventArgs
{
public string Text { get; set; }
public string Comment { get; set; }
public string Value { get; set; }
public DateTime Time { get; set; }
public string Type { get; set; }
}
The ReviewControl in the datatemplate had a DependencyProperty like so
public TagEvent ReviewEvent
{
get
{
return (TagEvent)GetValue(ReviewEventProperty);
}
set
{
SetValue(ReviewEventProperty, value);
}
}
public static readonly DependencyProperty ReviewEventProperty = DependencyProperty.Register("ReviewEvent", typeof(TagEvent), typeof(ReviewControl), new PropertyMetadata(new TagEvent() { Comment = "hallo", Text = "De tag", Time = DateTime.Now, Type = "Mark", Value = "Mark" }, ReviewEvent_PropertyChangedCallback));
private static void ReviewEvent_PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ReviewControl reviewControl = (ReviewControl)d;
reviewControl.LoadReviewEvent();
}
The strange thing is that when I run my progran the TagEvents get added to the collection and the ItemsControl shows the datatemplate for every item. The testblock I used to verify the value of the properties on the TagEvents shows the proper value of the text property. But in the ReviewEvent Dependency property I get only "empty" objects (all values empty string or default date). Those objects replace the default value as i can see that as the oldvalue in the DP callback.
I could understand the ItemsControl not showing the items, but why it is showing item that look like it's doing "new TagEvent" for every item in the collection is beyond me. Hope someone here has a suggestion for me. I tried implementing INotifyPropertyChanged on the TagEvent, but that did not seem to change anything. I could 开发者_运维技巧split out the properties of the TagEvent class but I don't see why I would have to do that, when I could pass the object.
Help?
I can now answer my own question. I had an statement setting the usercontrol's datacontext in code-behind that I forgot about. It messed things up.
Never leave old code laying about...
精彩评论