C# 如何绑定可观察集合以查看可观察集合中存在的项数?

C# 如何绑定可观察集合以查看可观察集合中存在的项数?,c#,wpf,xaml,observablecollection,C#,Wpf,Xaml,Observablecollection,大家好,我正在从事一个通过telnet配置网络设备的项目,我正在使用C#Wpf。问题是我有一个可观察的集合,我想显示可观察集合中存在的项目数,但它不显示。我尝试过以下方法: public ObservableCollection<VLANSPropertyClass> vlan { get; set; } public int Vlans { // Retreive value from Configuration Library

大家好,我正在从事一个通过telnet配置网络设备的项目,我正在使用C#Wpf。问题是我有一个可观察的集合,我想显示可观察集合中存在的项目数,但它不显示。我尝试过以下方法:

 public ObservableCollection<VLANSPropertyClass> vlan { get; set; }

  public int Vlans
        {
            // Retreive value from Configuration Library
            get
            {
                return this.vlan.Count;

            }
        }
publicobservableCollection vlan{get;set;}
公共int VLAN
{
//从配置库中检索值
得到
{
返回this.vlan.Count;
}
}
XAML是:

 <TextBlock Margin="3,0"  
        Style="{StaticResource SummaryValues}"  
        Text="{Binding Path=Vlans}"
        Visibility="Visible"
        />


现在它没有显示任何内容。任何帮助都是非常值得注意的:)

绑定不知道何时更新目标。您需要实现INotifyPropertyChanged、subscribe to.CollectionChanged并在VLAN(计数)更改时引发PropertyChanged事件。

绑定不知道何时更新目标。您需要实现INotifyPropertyChanged、订阅.CollectionChanged并在VLAN(计数)更改时引发PropertyChanged事件。

您可以在ObservableCollection顶部使用ListCollectionView

在ViewModel中,将其定义为:

    ObservableCollection<VLANSPropertyClass> vlan;
    public System.Windows.Data.ListCollectionView CountingView { get; private set; }
在xaml中,绑定到集合的Count属性:

CountingView = new System.Windows.Data.ListCollectionView(vlan);
<TextBlock Margin="5" Height="35" Width="50" Text="{Binding CountingView.Count}" />


我希望这有帮助

您可以在ObservableCollection顶部使用ListCollectionView

在ViewModel中,将其定义为:

    ObservableCollection<VLANSPropertyClass> vlan;
    public System.Windows.Data.ListCollectionView CountingView { get; private set; }
在xaml中,绑定到集合的Count属性:

CountingView = new System.Windows.Data.ListCollectionView(vlan);
<TextBlock Margin="5" Height="35" Width="50" Text="{Binding CountingView.Count}" />


我希望这有帮助

最简单的方法是简单地绑定到vlan.Count(在我的示例中,我使用了textbox,但textBlock不需要单向)。不需要更改额外的属性或InotifyProperty。添加到集合将自动更新计数

<TextBox Text="{Binding vlan.Count, Mode=OneWay}" />


最简单的方法是简单地绑定到vlan.Count(在我的示例中,我使用了textbox,但textBlock不需要单向)。不需要更改额外的属性或InotifyProperty。添加到集合将自动更新计数

<TextBox Text="{Binding vlan.Count, Mode=OneWay}" />



是的,我知道怎么做?是的,我知道怎么做?在调用vlan之前是否创建了vlan?您是否调试并看到了名为的行。在调用vlan之前是否创建了vlan?你调试过并看到那行代码了吗?但是为什么要使用文本框呢?最好使用文本块。然后,也不需要指定
Mode=OneWay
。使用您想要的任何控件,该点仅绑定到集合的计数。这只是一个示例textblock没有显示任何内容,它在后端控制台中显示计数数,但在我的视图中不显示。但是为什么要使用TextBox呢?最好使用文本块。然后,也不需要指定
Mode=OneWay
。使用您想要的任何控件,该点仅绑定到集合的计数。这只是一个示例textblock没有显示任何内容,它在后端控制台中显示计数数,但在我的视图中不显示。