【问题标题】:Refresh ListView grouped items刷新 ListView 分组项
【发布时间】:2012-09-09 01:32:18
【问题描述】:

我有一个ListView,它将GridView 实现为View,其中项目根据ComboBox 表示的字段进行分组(使用Expander)。

我的ListViewItemsSource 与以下的ObservableCollection 绑定:

public class data
{
   public string Name { get; set; }
   public int Age { get; set; }
   public bool Graduate { get; set; }
   public eMusic Music { get; set; }
}

public enum eMusic
{
    Rock,
    Pop,
    Classic
}

Music 属性的值由ComboBox 显示。

问题如下:当我更改 ComboBox 的值(例如 Pop -> Rock)时,相关项目不会在相应的组中移动。为什么?我怎么解决这个问题?

编辑:

MainWindow.xaml
MainWindow.xaml.cs

【问题讨论】:

    标签: c# wpf listview refresh grouping


    【解决方案1】:

    当我编辑ComboBox 时,我必须刷新我的ListViewItemsSource

    ICollectionView view = CollectionViewSource.GetDefaultView(MyListView.ItemsSource);
    view.Refresh();
    

    【讨论】:

      【解决方案2】:

      在组合框 xaml 属性中添加IsSynchronizedWithCurrentItem="True"

      像这样在你的视图模型上写一个属性

          private ICollectionView _musicsView;
          public ObservableCollection<Music> Musics{ get; private set; }
      
          public YourViewModel()
          { 
             Musics= new ObservableCollection<Music>();
             _music= CollectionViewSource.GetDefaultView(AvaibleLanguages);
             _music.CurrentChanged += new EventHandler(OnCurrentChanged);
          }
      
          public ICollectionView MusicsView
          {
             get
             {
                return _musicsView;
             } 
          }
      
          void OnCurrentChanged(object sender, EventArgs e)
          {
              YourObject current = _musicsView.CurrentItem as YourItem;
      //do: what you want
          }
      

      【讨论】:

      • 它不起作用,使用 IsSynchronizedWithCurrentItem="True" 当我更改 ComboBox(Pop -> Rock)时,所有 ComboBox 值都变为 Rock。
      猜你喜欢
      • 2012-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多