【问题标题】:StatusBar record x of yStatusBar 记录 x of y
【发布时间】:2014-05-14 17:44:36
【问题描述】:

我有一个 ObservableCollection,它是网格的 DataContext。 Grid 是插入到主窗口中的用户控件。

我想在状态栏中显示“记录 x of y”,因此,作为第一步,我尝试使用此 XAML 在网格中显示它:

<TextBlock Grid.Row="2" Grid.Column="3">
    <TextBlock Text="{Binding CurrentPosition}" /> <TextBlock Text="{Binding Count}" />
</TextBlock>

Count 可以正常工作,并且会在添加新项目时自动更新。 CurrentPosition(在我下面的代码中定义)始终保持为 0。

如何使 CurrentPosition 自动更新?我希望不必使用 INotify**,因为这已经是 ObservableCollection。

我也没有任何代码隐藏,所以我希望它可以在我的类(或模型)和 XAML 中实现。

我确实尝试过使用 CurrentChanged 但没有成功:

    public MyObservableCollection() : base() {
        this.GetDefaultView().CurrentChanged += MyObservableCollection_CurrentChanged;
    }

MyObservableCollection:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace ToDoApplication.Models {
    public class MyObservableCollection<T> : ObservableCollection<T> {

        public MyObservableCollection() : base() {
        }

        public MyObservableCollection(List<T> list) : base(list) {
        }

        public MyObservableCollection(IEnumerable<T> collection) : base(collection) {
        }

        private System.ComponentModel.ICollectionView GetDefaultView() {
            return System.Windows.Data.CollectionViewSource.GetDefaultView(this);
        }

        public int CurrentPosition {
            get {
                return this.GetDefaultView().CurrentPosition;
            }
        }

        public void MoveFirst() {
            this.GetDefaultView().MoveCurrentToFirst();
        }
        public void MovePrevious() {
            this.GetDefaultView().MoveCurrentToPrevious();
        }
        public void MoveNext() {
            this.GetDefaultView().MoveCurrentToNext();
        }
        public void MoveLast() {
            this.GetDefaultView().MoveCurrentToLast();
        }

        public bool CanMoveBack() {
            return this.CurrentPosition > 0;
        }
        public bool CanMoveForward() {
            return (this.Count > 0) && (this.CurrentPosition < this.Count - 1);
        }
    }

    public enum Navigation {
        First, Previous, Next, Last, Add
    }
}

更新:我正在添加以下代码作为可能的解决方案,但我不太喜欢它,我希望出现一个不需要我使用的更好的代码INotifyPropertyChanged - 我怀疑我将结束重复 ObservableCollection 应该已经可用的所有功能。 (我也不知道为什么要重新通知 Count 的变化。)

更新 2:以下不是(完整)解决方案,因为它会干扰集合的其他行为(通知),但我将其保留在这里以防它包含任何有用信息.

namespace ToDoApplication.Models {
    public class MyObservableCollection<T> : ObservableCollection<T>, INotifyPropertyChanged {
        public new event PropertyChangedEventHandler PropertyChanged;
        private int _currentPos = 1;

        public MyObservableCollection() : base() {
            this.GetDefaultView().CurrentChanged += MyObservableCollection_CurrentChanged;
            this.CollectionChanged += MyObservableCollection_CollectionChanged;
        }

        public MyObservableCollection(List<T> list) : base(list) {
            this.GetDefaultView().CurrentChanged += MyObservableCollection_CurrentChanged;
            this.CollectionChanged += MyObservableCollection_CollectionChanged;
        }
        public MyObservableCollection(IEnumerable<T> collection) : base(collection) {
            this.GetDefaultView().CurrentChanged += MyObservableCollection_CurrentChanged;
            this.CollectionChanged += MyObservableCollection_CollectionChanged;
        }

        void MyObservableCollection_CurrentChanged(object sender, EventArgs e) {
            this.CurrentPosition = this.GetDefaultView().CurrentPosition;
        }
        void MyObservableCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
            RaisePropertyChanged("Count");
        }

        private System.ComponentModel.ICollectionView GetDefaultView() {
            return System.Windows.Data.CollectionViewSource.GetDefaultView(this);
        }

        public int CurrentPosition {
            get {
                return _currentPos;
            }
            private set {
                if (_currentPos == value + 1) return;
                _currentPos = value + 1;
                RaisePropertyChanged("CurrentPosition");
            }
        }

        private void RaisePropertyChanged(string propertyName) {
            if (PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public void MoveFirst() {
            this.GetDefaultView().MoveCurrentToFirst();
        }
        public void MovePrevious() {
            this.GetDefaultView().MoveCurrentToPrevious();
        }
        public void MoveNext() {
            this.GetDefaultView().MoveCurrentToNext();
        }
        public void MoveLast() {
            this.GetDefaultView().MoveCurrentToLast();
        }

        public bool CanMoveBack() {
            return this.CurrentPosition > 1;
        }
        public bool CanMoveForward() {
            return (this.Count > 0) && (this.CurrentPosition < this.Count);
        }
    }

    public enum Navigation {
        First, Previous, Next, Last, Add
    }
}

这样我可以在网格中显示“第 1 项,共 3 项”:

    <TextBlock Grid.Row="2" Grid.Column="3" x:Name="txtItemOf">Item 
            <TextBlock x:Name="txtItem" Text="{Binding CurrentPosition}" /> of 
            <TextBlock x:Name="txtOf" Text="{Binding Count}" />
    </TextBlock>

我不再需要这个 TextBlock,因为我可以直接在(主)StatusBar 中引用 DataContext 属性:

    <StatusBar DockPanel.Dock="Bottom">
        Item <TextBlock Text="{Binding ElementName=vwToDo, Path=DataContext.CurrentPosition}" />
        Of <TextBlock Text="{Binding ElementName=vwToDo, Path=DataContext.Count}" />
    </StatusBar>

问题与解决方案

按照@JMarsch 的回答:命名我的属性CurrentPosition 是在屏蔽已经直接从DataContext 中可用的同名属性,因为绑定是到集合的默认视图(具有此属性)。

解决方案是重命名为MyCurrentPosition,并从状态栏中引用原始属性,或者像我一样,完全删除我的这个属性版本(以及GetDefaultView):他们没有这样做任何特别有用的东西。

然后我使用以下简单的 ValueConverter 将 StatusBar 中的 0,1,2,.. 转换为 1,2,3,..。

[ValueConversion(typeof(int), typeof(int))]
class PositionConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        return (int)value + 1;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        return (int)value - 1;
    }
}

状态栏:

    <StatusBar DockPanel.Dock="Bottom" x:Name="status">
        Item <TextBlock Text="{Binding ElementName=vwToDo, 
            Path=DataContext.CurrentPosition, Converter={StaticResource posConverter}}" />
        Of <TextBlock Text="{Binding ElementName=vwToDo, Path=DataContext.Count}" />
    </StatusBar>

【问题讨论】:

  • 基础集合的 Xaml 是否声明它与当前项同步?将此设置为 true 会对集合视图产生有益的副作用,可能会给您带来更好的结果。
  • IsSynchronizedWithCurrentItem 适用于 ItemsControl,例如 ListBox。我没有使用其中之一,因此该属性不可用。 (我使用 Next,Previous 按钮在项目之间移动。)
  • 感谢您的链接,但我认为没有什么可以用于我的情况。

标签: c# wpf


【解决方案1】:

关于数据绑定到集合的非常重要的一点是 XAML总是 数据绑定到集合视图,而不是集合本身。因此,即使您的 XAML 似乎绑定到集合,但在运行时,您实际上是绑定到默认集合视图。

这里有一个很酷的副作用:collectionview 已经有一个 CurrentPosition 属性。我认为事情对你不利,因为你无意中干预了你的收藏。

下面是一个非常快速而肮脏的小程序,它说明了对 CurrentPostion 和 Count 的有效绑定,而无需在集合上定义当前位置(因为在幕后,您实际上是绑定到 CollectionView,并且它已经有一个通知更改的 CurrentPosition 属性。

运行此程序,注意当您单击增量按钮时,UI 会相应更新。

这是 XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel x:Name="ContentPanel">
        <Button x:Name="IncrementButton" Content="Increment" Click="IncrementButton_Click"/>
        <StackPanel Orientation="Horizontal">
            <TextBlock x:Name="CurrentPositionTextBlock" Text="{Binding CurrentPosition}"/>
            <TextBlock Text=" / "/>
            <TextBlock Text="{Binding Count}"/>
        </StackPanel>
    </StackPanel>
</Window>

这是代码隐藏:

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Collection = new TestObservableCollection<object>() {new object(), new object(), new object()};
            this.ContentPanel.DataContext = this.Collection;
        }

        public TestObservableCollection<object> Collection { get; set; }

        private void IncrementButton_Click(object sender, RoutedEventArgs e)
        {
            this.Collection.GetDefaultView().MoveCurrentToNext();
        }
    }

    public class TestObservableCollection<T> : ObservableCollection<T>
    {
        public ICollectionView GetDefaultView()
        {
            return CollectionViewSource.GetDefaultView(this);
        }
    }
}

这里有更多阅读内容:

http://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/system.windows.data.collectionviewsource(v=vs.110).aspx

编辑

如果您想进一步证明发生了什么,请将下面的代码粘贴到我的按钮单击处理程序中——您会看到文本框绑定到的实际对象类型是 ListCollectionView,而不是实际的集合:

System.Diagnostics.Debug.WriteLine(this.CurrentPositionTextBlock.GetBindingExpression(TextBlock.TextProperty).ResolvedSource.GetType().FullName);

【讨论】:

  • 这行得通,谢谢。本质上,我的 CurrentPosition 版本掩盖了我已经可用的默认版本。但是,它显示的是 0 而不是 1。请问如何将值加 1?如果没有这个,我可能还必须为我的每个方法使用 PropertyChanged 事件,这会很痛苦。
  • 不用担心,ValueConverter 可以完成这项工作。不过,我不会就赏金做出决定,我需要研究其他建议。
  • 值转换器是我所建议的!听起来你已经完成了!
  • 您可以做的另一件事是故意显示一个 CollectionView 而不是集合,并明确地绑定到它。集合视图的目的是充当控件和集合之间的中间人。该视图添加了当前记录、过滤和排序的概念。如果需要,您可以有 2 个不同的视图,具有不同的排序选项和不同的当前记录值,它们都来自同一个基础集合。
【解决方案2】:

您的 Count 绑定正在更新,因为基础 ObservableCollection&lt;T&gt; 在添加和删除项目时引发 PropertyChanged

当您重新定位当前记录指针时,您的定位代码需要引发PropertyChanged,以便绑定子系统知道重新查询属性,这就是INotifyPropertyChanged 存在的原因。不过,我可能会像下面这样写。请注意使用 ObservableCollections&lt;T&gt;OnPropertyChanged 从继承树中已经实现的 INotifyPropertyChanged 引发正确的事件。

        public void MoveFirst() {
            this.GetDefaultView().MoveCurrentToFirst();
            OnPropertyChanged(new PropertyChangedEventArgs("CurrentPosition"));
        }
        public void MovePrevious() {
            this.GetDefaultView().MoveCurrentToPrevious();
            OnPropertyChanged(new PropertyChangedEventArgs("CurrentPosition"));
        }
        public void MoveNext() {
            this.GetDefaultView().MoveCurrentToNext();
            OnPropertyChanged(new PropertyChangedEventArgs("CurrentPosition"));
        }
        public void MoveLast() {
            this.GetDefaultView().MoveCurrentToLast();
            OnPropertyChanged(new PropertyChangedEventArgs("CurrentPosition"));
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-02
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    相关资源
    最近更新 更多