【问题标题】:How update List of datagrid c# wpf caliburn.microc# wpf caliburn.micro如何更新datagrid列表
【发布时间】:2013-04-05 10:55:57
【问题描述】:

在这个类中:

 [Export(typeof(IScreen))]
    public class BolleViewModel : Screen
    {
              ....
    }

我有这个列表:

public List<Article> List { get; private set; }

这个列表是Datagrid到List的绑定:

<DataGrid HorizontalAlignment="Stretch" SelectedItem="{Binding SelectedArticle}"
            Margin="14,41,12,61" VerticalAlignment="Stretch" AutoGenerateColumns="False" x:Name="List">

我希望当我调用 UPDATE 方法时,更新 List 和 Datagrid 的值。 这是我的更新方法:

    public void Update(List<Article> list)
    {
        List = list;
        NotifyOfPropertyChange("List");
    }

我错了什么? ?

【问题讨论】:

    标签: c# wpf list datagrid caliburn.micro


    【解决方案1】:

    Caliburn.Micro 不支持开箱即用的DataGrid 的基于约定的绑定,您可以通过检查ConventionManager static constructor 来查看这一点。

    您可以使用ConventionManager 编写自己的约定,也可以在视图中设置ItemsSource 属性绑定。

    例如

    <DataGrid ItemsSource="{Binding Articles}" SelectedItem="{Binding SelectedArticle}"
            Margin="14,41,12,61" AutoGenerateColumns="False"
            HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> ...
    

    其他要点:

    1. List 对于您的文章列表来说不是一个很好的属性名称
    2. Caliburn.Micro 为 NotifyOfPropertyChange 提供基于 lambda 的覆盖,您应该使用它来捕获重构
    3. 实现 INPC 属性的更好模式如下(这是因为更改属性以调用 PropertyChanged 事件的消费者不再负责)

    用途:

    private List<Article> articles;
    
    public List<Article> Articles 
    { 
       get 
       {
           return this.articles;
       }
    
       private set
       {
           if (this.articles == value)
           {
               return;
           } 
    
           this.articles = value;
           this.NotifyOfPropertyChange(() => this.Articles);
       }
    }
    

    由于这是一个集合类型,您还应该确保始终返回一个集合而不是 null。这避免了消费者需要检查以避免空引用异常。

    【讨论】:

    • 如果我不将 caliburn.micro 用于此数据网格,我如何更改我的代码?
    • 好的。但不要工作。我在另一个方法中更改了项目列表,然后调用更新方法以确保数据网格显示新列表,但不起作用!公共无效更新(列表列表){文章=列表; }
    • 您的方法似乎有点复杂。您从列表视图模型中获得了对所选文章的引用,您将其传递到详细视图模型中。您可以在详细视图模型中更新该参考的属性。您也不应该使用公共字段。将它们设为您希望绑定的属性,并将应为私有的字段设为私有。
    【解决方案2】:

    这是Datagrid视图的viewModel:

        [Export(typeof(IScreen))]
        public class BViewModel : Screen
        {
                private List<Article> articles;
                public List<Article> Articles
                {
                    get
                    {
                        return this.articles;
                    }
    
                    private set
                    {
                        if (this.articles == value)
                        {
                            return;
                        }
    
                        this.articles = value;
                        this.NotifyOfPropertyChange(() => this.Articles);
                    }
                }
    
           public BolleViewModel()
           {
             Articles = recoverArticles(); //returns a list of articles
           }
    
    
           public void Update(List<Article> list)
           {
                   Articles = list;
           }
    
            //is associated with a button
            public void Detail()
            {
                if(SelectedArticle!=null)
                    WindowManager.ShowWindow(new DetailArticleViewModel(SelectedArticle, Articles), null, null);
                else
                {
                    MessageBox.Show("Select an article","Error!",MessageBoxButton.OK,MessageBoxImage.Error);
                }
            }
    
    
       }
    

    DetailArticleViewModel 更改Articles 列表中的一项并调用BViewModel 的Update 方法。

    [Export(typeof(IScreen))]
        public class DetailArticleViewModel : Screen
        {
          public List<Article > GeneralList;
          public Article ArticleSelected;
          public BViewModel bw;
    
    
          public DetailArticleViewModel(Article art,List<Article> arts,BViewModel viewmodel)
          {
              ArticleSelected = art;
              GeneralList = arts;
              bw = viewmodel;
          }
    
          // is associated with a button
          public void Save()
          {
              var index = GeneralList.FindIndex(item => item.Code.CompareTo(ArticleSelected.Code)==0);
              GeneralList[index].Price = 900;
              bw.Update(List);
    
          }
    
        }
    

    但是精选文章的价格不是900!为什么?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      相关资源
      最近更新 更多