I am developing a WPF project and am experimenting a very strange behaviour.
I have added a RibbonComboBox
to the Ribbon and inside of it there is a RibbonGallery
which has a RibbonGalleryCategory
that has a binding to an array of elements.
<rb:RibbonComboBox Name="xComboBox" Label="List:" >
<rb:RibbonGallery SelectedValue="{Binding SelectedValue, Mode=TwoWay}">
<rb:RibbonGalleryCategory ItemsSource="{Binding List}" />
</rb:RibbonGallery>
</rb:RibbonComboBox>
Everything works just fine so far, when I run the program the RibbonComboBox has its items as expected.
The problem starts when I resize the container window to a very small size, after doing that and resizing it back, the ComboBox is empty!!
I have no idea why this is happening, am I doing something wrong??
I have tried to see what is going on, so, I added an event to the Items
property of the RibbonGalleryCategory
as follow:
public RibbonView()
{
InitializeComponent();
RibbonGallery gallery = xComboBox.Items[0] as RibbonGallery;
RibbonGalleryCategory galleryCat = gallery .Items[0] as RibbonGalleryCategory;
((INotifyCollectionChanged)galleryCat.Items).CollectionChanged += new NotifyCollectionChangedEventHandler(RibbonView_CollectionChanged);
}
void RibbonView_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
Dispatcher.BeginInvoke(new Action(() =>
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
MessageBox.Show("Collection has changed >>>> Add");
break;
case NotifyCollectionChangedAction.Move:
MessageBox.Show("Collection has changed >>>> Move");
break;
case NotifyCollectionChangedAction.Remove:
MessageBox.Show("Collection has changed >>>> Remove");
break;
case NotifyCollectionChangedAction.Replace:
MessageBox.Show("Collection has changed >>>> Replace");
break;
case NotifyCollectionChangedAction.Reset:
MessageBox.Show("Collection has changed >>>> Reset");
break;
}
}), System.Windows.Threading.DispatcherPriority.Background, null);
}
As you can see, I show what is the change in the collection, so, after running the program and resizing the window, my little test tells me that the collection was "reset"!!
Does anyone know why this happens?? How can I prevent the RibbonComboBox from losing its data??
Thank you in advance.
EDIT:
More info: I just noticed something, after resizing the container window the RibbonComboBox changes its DataContext for an object called "{DisconnectedItem}
". I have done some research and found this. But I still dont know how to prevent from it.
Does anybody know how to avoid that a control loses its DataContext (which makes the combobox lose its data)?