结合所选行数到文本块不触发DataGrid的滚动后OnPropertyChanged [英] Binding Selected RowCount to TextBlock not Firing OnPropertyChanged after DataGrid Scroll

查看:200
本文介绍了结合所选行数到文本块不触发DataGrid的滚动后OnPropertyChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有,我最近遇到一个问题,当所选行滚动时, DataGrid 的视觉效果没有更新( WPF DataGrid列在使用多选择时被损坏)回答。

All, I have recently had a problem where the visuals of a DataGrid were not updating when the selected rows were scrolled (WPF DataGrid Column Becoming Corrupt when using Multi-Selection) which was solved using the method in the linked answer.

我在 StatusBar 中有一个 TextBlock 将此绑定到所需的ViewModel的 IsSelected 属性。我认为上述问题的解决方案也将解决所选行数未被正确更新的事实;也就是说,当 DataGrid 滚动条不移动时,它会更新,但是当移动条时, TextBlock 无法更新为 OnPropertyChanged 事件未触发。我检查了很多的帖子,并设置 IsAsync = true ,这有助于使选择正确(这是更新之前1)。相关的XMAL是

I have a TextBlock in a StatusBar and I bind this to an IsSelected property to the required ViewModel. I thought that a solution to the question above would also solve the fact that the selected row count was not being updated correctly; that is, it updates fine when the DataGrid scroll bar does not move, but when the bar does move, the TextBlock fails to update as the OnPropertyChanged event is not fired. I have check lots of posts on this and set the IsAsync=true which did help in making the selection correct (it was 1 out before this update). The relevant XMAL is

<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 滚动时停止更新,即使在这种情况下,如何停止并允许行计数显示? strong>

TotalSelectedCultures stops updating when the DataGrid scrolls, how do I stop this and allow the row count to display even in this case?

感谢您的时间。

推荐答案

附加到LoadCultures()中的SelectedItems.CollectionChanged事件?

Can you simply attach to the SelectedItems.CollectionChanged event inside LoadCultures()?

Cultures.SelectedItems.CollectionChanged += SelectedItems_CollectionChanged;

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

这篇关于结合所选行数到文本块不触发DataGrid的滚动后OnPropertyChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆