2013-07-08 61 views
2

我最近遇到了一個問題,其中DataGrid的視覺效果在所選行滾動時沒有更新(WPF DataGrid Column Becoming Corrupt when using Multi-Selection),這是使用鏈接答案中的方法解決的。綁定選定的RowCount到TextBlock沒有在DataGrid滾動後觸發OnPropertyChanged滾動

我在StatusBar中有一個TextBlock,我將它與IsSelected屬性綁定到所需的ViewModel。我認爲上述問題的解決方案也解決了選定的行數未被正確更新的事實;也就是說,當DataGrid滾動條不移動時它會更新正常,但當條形圖移動時,TextBlock無法更新,因爲OnPropertyChanged事件未觸發。我已經檢查了很多帖子,並設置了IsAsync=true,這有助於使選擇正確(在此更新之前已有1個)。相關XMAL是

<DataGrid Grid.Row="1" AlternatingRowBackground="Gainsboro" AlternationCount="2" 
      HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
      AutoGenerateColumns="False" RowHeaderWidth="0" IsReadOnly="True" 
      CanUserAddRows="False" CanUserDeleteRows="False" SelectionMode="Extended" 
      EnableRowVirtualization="True" EnableColumnVirtualization="True" 
      ItemsSource="{Binding Cultures, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged, IsAsync=True}" 
      dataAccess:MultiSelectItem.IsEnabled="True"> 
<DataGrid.Columns> 
    <DataGridTextColumn Header="Code" Binding="{Binding Code}" IsReadOnly="True"/> 
    <DataGridTextColumn Header="Language" Binding="{Binding Language}" IsReadOnly="True"/> 
    <DataGridTextColumn Header="LocalName" Binding="{Binding LocalName}" IsReadOnly="True"/> 
</DataGrid.Columns> 
</DataGrid> 

<StatusBar Grid.Row="1" Margin="0,0.4,0.4,-0.4"> 
    <StatusBarItem DockPanel.Dock="Left" Background="#FF007ACC" Margin="0,2,0,0"> 
     <TextBlock Text="{Binding TotalSelectedCultures}" Margin="5,0,0,0" Foreground="White"/> 
    </StatusBarItem> 
</StatusBar> 

凡視圖模型,這個鏈接是

public class CultureDataViewModel : ViewModelBase 
{ 
    public enum FilterType 
    { 
     AllCultures, 
     NeutralCultures, 
     SpecificCultures 
    }; 
    public FilterType SelectedFilterType { get; private set; } 
    public ICollectionView CulturesView { get; private set; } 
    public MultiSelectCollectionView<CultureViewModel> Cultures { get; private set; } 

    public CultureDataViewModel() 
    { 
     SelectedFilterType = FilterType.AllCultures; 
     LoadCultures(); 
    } 

    void OnCultureViewModelPropertyChanged(object sender, PropertyChangedEventArgs e) 
    { 
     string IsSelected = "IsSelected"; 
     (sender as CultureViewModel).VerifyPropertyName(IsSelected); 
     if (e.PropertyName == IsSelected) 
      this.OnPropertyChanged("TotalSelectedCultures"); 
    } 

    void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
    { 
     if (e.NewItems != null && e.NewItems.Count != 0) 
      foreach (CultureViewModel cultVm in e.NewItems) 
       cultVm.PropertyChanged += this.OnCultureViewModelPropertyChanged; 

     if (e.OldItems != null && e.OldItems.Count != 0) 
      foreach (CultureViewModel cultVm in e.OldItems) 
       cultVm.PropertyChanged -= this.OnCultureViewModelPropertyChanged; 
    } 

    /// <summary> 
    /// Load the avalible cultures depending on filter selection 
    /// </summary> 
    public void LoadCultures() 
    { 
     // Get data... 
    } 

    /// <summary> 
    /// Hold the current total selected cultures to add to the resource 
    /// data set. 
    /// </summary> 
    public string TotalSelectedCultures 
    { 
     get 
     { 
      int selectedCultures = this.Cultures.SelectedItems.Count; 
      return String.Format("{0:n0} of {1:n0} cultures selected", 
             selectedCultures, 
             Cultures.Count); 
     } 
    } 
} 

TotalSelectedCultures停止更新時DataGrid卷軸,我該如何制止這種並允許行數,即使在顯示這個案例?

謝謝你的時間。

回答

0

你可以簡單地附加到LoadCultures()裏面的SelectedItems.CollectionChanged事件嗎?

Cultures.SelectedItems.CollectionChanged += SelectedItems_CollectionChanged; 

void SelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
{ 
    this.RaisePropertyChanged("TotalSelectedCultures"); 
} 
+0

這打破了MVVM模式... – MoonKnight

+0

你能解釋一下嗎?在這種情況下,視圖模型會偵聽(它自己的viewmodel集合文化)SelectedItems屬性(已經綁定在您的視圖中),並且簡單地宣佈TotalSelectedCultures已更改 - 這將強制綁定到此屬性的任何視圖進行更新。 – sfm