使用Mvvm-light工具包为WP7“刷新”数据透视控件

时间:2010-11-02 12:45:59

标签: windows-phone-7 pivot mvvm-light

我在我的Xaml中有一个转轴控件:

    <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>    

我的ViewModel是:

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));
    }
}

当达到timer_tick时,我认为枢轴控制用新值刷新......但我看不到任何变化。

我想念什么?

提前感谢您的帮助

1 个答案:

答案 0 :(得分:2)

我猜测可能更新列表成员而不更新列表本身就是问题所在。当您举起属性更改事件时 - 它适用于整个集合。尽管成员已经改变,但该集合仍然指向自己的平等参考。

尝试在setter中放置一个断点,看看是否触发了属性更改事件。