【问题标题】:execute a function in a viewmodel and real time refresh of the view在视图模型中执行功能并实时刷新视图
【发布时间】:2013-05-29 17:36:04
【问题描述】:

我的 MVVM 应用程序有一个小问题。

我在视图模型中有一个修改集合的函数。此集合绑定到视图以显示数据网格。当用户点击按钮时,该函数会修改集合,但可能需要几分钟时间,并且视图不会刷新。

我的问题是如何执行此功能以实时刷新视图?

在另一个程序中,我使用了调度程序,但它位于视图后面的代码中,没有绑定。

谢谢

编辑:

型号:

public class Composants : INotifyPropertyChanged
{
    private string _nom;

    public string Nom
    {
        get { return _nom; }
        set { _nom = value; OnPropertyChanged("Nom"); }
    }

}

视图模型:

public class PageSynchroViewModel : INotifyPropertyChanged
{
    public void SynchroniserComposants()
    {
        foreach (var comp in _selectedVersion.ListeComposants)
        {
            comp.Nom = "";
        }
}

查看(我没有放所有代码):

<Page x:Class="Centre_de_synchronisation.Vues.PageSynchro"
  [...]
  xmlns:app="clr-namespace:Centre_de_synchronisation.Classes" mc:Ignorable="d"
  d:DesignHeight="531" d:DesignWidth="778"
Title="PageSynchro" Background="{x:Null}">
<Canvas>
    [...]
    <DataGrid Name="GridComposants" Style="{StaticResource DatagridStyle}"  ItemsSource="{Binding ListeComposants}" AutoGenerateColumns="False" Canvas.Left="12" Canvas.Top="201" Height="285"  Width="754"  >
        <DataGrid.Columns>
            <DataGridTextColumn
       Header="Nom"
       Binding="{Binding Nom}"
       Width="150"
               IsReadOnly="True"/>
            [...]
    </DataGrid>
    <Button Name="BoutonSynchro" Style="{StaticResource MessageBoxButtonStyle}" Content="Synchroniser" Height="27" Width="107" Command="{Binding BoutonSynchro}" CommandParameter="GridComposants" Visibility="{Binding Etat, Converter={StaticResource VisibilityConverter}}"/>
</Canvas>

【问题讨论】:

  • 分钟?你能发布你的代码吗?
  • 几秒钟,但用户在单击“同步”按钮时没有任何信息。我在 MainWindow 上有一个小的加载指示器。当用户单击同步按钮时,PageSynchroViewModel 会向 MainWindow(带有 mvvm 灯)发送一条消息以显示加载指示器,但它不显示。

标签: wpf mvvm mvvm-light


【解决方案1】:

尝试使用ObservableCollection&lt;T&gt; 而不是您现在使用的集合。

这应该会导致在集合中添加或删除项目时更新视图。

请记住在与ObservableCollection 交互时调用调度程序,否则您将获得线程访问异常

这是我用来测试的代码。

XAML

<Window.Resources>
    <loc:MyViewModel x:Key="ViewModel" />
</Window.Resources>
<Canvas DataContext="{StaticResource ViewModel}">
    <DataGrid ItemsSource="{Binding Collection}"
              Width="150"
              Height="200">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Nom"
                                Binding="{Binding Nom}"
                                Width="150"
                                IsReadOnly="True" />
        </DataGrid.Columns>
    </DataGrid>
    <Button Command="{Binding DoStuffCommand}"
            Canvas.Bottom="0"
            Canvas.Right="0">Stuff</Button>
</Canvas>

视图模型

public class MyViewModel
{
    public ObservableCollection<MyModel> Collection { get; set; }

    public ICommand DoStuffCommand { get; set; }

    public MyViewModel()
    {
        this.Collection = new ObservableCollection<MyModel>();

        for (int i = 0; i < 10; i++)
        {
            Collection.Add(new MyModel { Nom = i.ToString() });
        }

        this.DoStuffCommand = new RelayCommand(DoStuff);
    }

    private void DoStuff()
    {
        foreach (var item in Collection)
        {
            item.Nom = item.Nom + ".";
        }
    }
}

型号

public class MyModel : INotifyPropertyChanged
{
    private string nom;

    public string Nom
    {
        get { return nom; }
        set
        {
            nom = value;
            RaiseChanged("Nom");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaiseChanged(string propertyName)
    {
        var handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

它更新了视图中的 Nom。

【讨论】:

  • 我不添加或删除项目,只是更改项目属性,每个属性都会调用 propertyChanged 事件。我用于集合的模型实现了 INotifyPropertyChange
  • 在处理结束时尝试使用集合名称调用 PropertyChanged 事件,如果您在集合项目上使用 INotifyPropertyChanged,它们应该通过 ObservableCollection 冒泡
  • 应该出现在您的视图模型中。尽管您可能希望使用相关代码更新您的问题。它可能会吸引更多的答案。
  • 您需要在要添加到 ObservableCollection 的每个项目上注册 PropertyChanged 事件 :)
  • @user1069516 我已经用一些代码更新了我的答案,你可能会看到你错过了什么。
猜你喜欢
  • 2011-09-17
  • 2021-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-16
相关资源
最近更新 更多