“刷新”使用适用于 WP7 的 Mvvm-light 工具包进行枢轴控制

发布于 2024-09-29 22:27:19 字数 2306 浏览 0 评论 0原文

我的 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 时,我认为枢轴控件会使用新值刷新...但我看不到任何更改。

我想念什么?

预先感谢您的帮助

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

注定孤独终老 2024-10-06 22:27:19

我猜测可能是在不更新列表本身的情况下更新列表的成员才是问题所在。当您引发属性更改事件时 - 它适用于整个集合。尽管成员已经发生变化,但该系列仍然指向自身的同等参考。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文