0

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

4

1 回答 1

2

尝试使用 anObservableCollection<T>而不是您现在使用的集合。

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

请记住在与ObservableCollectionInvoke Dispatcher 交互时,否则您将获得线程访问异常

这是我为测试它所做的代码。

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。

于 2013-05-28T12:06:24.633 回答