3

I have in my Xaml a pivot control :

    <controls:Pivot ItemsSource="{Binding ObjectList}">
        <controls:Pivot.HeaderTemplate>
            <DataTemplate>
                <TextBlock />
            </DataTemplate>
        </controls:Pivot.HeaderTemplate>
        <controls:Pivot.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Value1}" />
                    <TextBlock Text="{Binding Value2}" />
                </StackPanel>
            </DataTemplate>
        </controls:Pivot.ItemTemplate>
    </controls:Pivot>    

My ViewModel is :

public class MyObject
{
    public string Value1 { get; set; }
    public string Value2 { get; set; }
}

public class MyViewModel : ViewModelBase
{
    public const string ObjectListPropertyName = "ObjectList";
    private ObservableCollection<MyObject> _objectList;
    public ObservableCollection<MyObject> ObjectList
    {
        get
        {
            return _objectList;
        }

        private set
        {
            if (_objectList == value)
                return;
            _objectList = value;
            RaisePropertyChanged(ObjectListPropertyName);
        }
    }

    private DispatcherTimer timer;

    public MyViewModel()
    {
        ObservableCollection<MyObject> collection = new ObservableCollection<MyObject>
                          {
                              new MyObject {Value1 = "One"},
                              new MyObject {Value1 = "Two"},
                              new MyObject {Value1 = "Tree"}
                          };
        ObjectList = collection;
        timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2)};
        timer.Tick += timer_Tick;
        timer.Start();
    }

    void timer_Tick(object sender, EventArgs e)
    {
        foreach (MyObject myObject in _objectList)
        {
            myObject.Value2 = "Something";
        }
        Application.Current.RootVisual.Dispatcher.BeginInvoke( () => RaisePropertyChanged(ObjectListPropertyName));
    }
}

When the timer_tick is reached, I supposed the pivot control to refresh with the new values ... but I can't see any changes.

What do I miss ?

Thanks in advance for your help

Tim
  • 1,749
  • 3
  • 24
  • 48

1 Answers1

2

I'm guessing that possibly updating the members of the list without updating the list itself is the problem. When you raise the property changed event - it is for the entire collection. The collection is still pointing to an equal reference of itself, despite the fact that the members have changed.

Try placing a breakpoint in the setter and see if the property changed event is fired.

Chazmanian
  • 221
  • 1
  • 1
  • you seem to be right, if I do : for (int i = 0; i < _objectList.Count; i++ ) { _objectList[i] = new MyObject { Value1 = _objectList[i].Value1, Value2 = "Something" }; } it's working ... – Tim Nov 02 '10 at 13:22
  • I was thinking that perhaps you can create a new list of MyObjects, populate that with the new values (with foreach or for-next, either way), and then set the original list to this new temporary list. that would, in effect, change the list and allow the event to fire. It's not pretty, but given the example, it should work. – Chazmanian Nov 02 '10 at 14:29