0

in my View, there are a DataGrid and a TextBox, which is bound to the DataGrid's Items.Count property:

<DataGrid x:Name="dataGrid" ItemsSource="{Binding dataTable}"/>
<TextBox Text="{Binding Items.Count,ElementName=dataGrid,Mode=OneWay,StringFormat={}{0:#}}"/>

The ViewModel has a property (e.g. ItemsCount) which I'd like to be bound to the Items.Count property of the DataGrid, but have no idea, how to achieve this.

class ViewModel : INotifyPropertyChanged
{
    public DataTable dataTable {get;set;}
    public int ItemsCount {get;set;}
}

Maybe I could also use the Rows.Count property of the DataTable the DataGrid is bound to, but how would i bind or link the two properties in the ViewModel?

So I basically want the ItemsCount property to be synchronized with the dataTable.Rows.Count property.

THW
  • 55
  • 1
  • 2
  • 6
  • I assume you have your DataGrid's ItemsSource bound to some sort of collection property within your viewmodel. Why not bind the TextBox' Text-property to that collection's Count-Property? – i know nothing Jun 27 '14 at 11:02
  • Yes, it is bound to a DataTable. Binding the TextBox to dataTable.Rows.Count doesn't update the TextBox when items are added or deleted. Also this would not help to update the other property in the ViewModel. I've added the ViewModel's source – THW Jun 27 '14 at 12:50
  • http://stackoverflow.com/questions/3499903/how-to-get-items-count-from-collectionviewsource/41091043#41091043 – EeNiArT Dec 15 '16 at 19:30

1 Answers1

2

A common way to achieve your requirements are to declare properties to data bind to the UI controls:

<DataGrid x:Name="dataGrid" ItemsSource="{Binding Items}" />
<TextBox Text="{Binding ItemsCount}" />

...

// You need to implement the INotifyPropertyChanged interface properly here

private ObservableCollection<YourDataType> items = new ObservableCollection<YourDataType>();
public ObservableCollection<YourDataType> Items
{
    get { return items; }
    set { items = value; NotifyPropertyChanged("Items"); NotifyPropertyChanged("ItemCount"); }
}

public string ItemCount
{
    get { Items.Count.ToString("{0:#}"); }
}

UPDATE >>>

As @Sivasubramanian has added his own requirement to your question, in case you need to update the item count specifically by adding to your collection, you can manually call the NotifyPropertyChanged method:

Items.Add(new YourDataType());
NotifyPropertyChanged("ItemCount");
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • When we add an item to the "items" collection it will not trigger NotifyPropertyChange in items and so in ItemCount – Sivasubramanian Jun 27 '14 at 09:09
  • Was that a requirement? Even so, in that case, you could manually call `NotifyPropertyChanged("Items"); NotifyPropertyChanged("ItemCount");`... no problem. – Sheridan Jun 27 '14 at 09:12
  • What's the problem with this solution, mystery down voter? Care to comment to make you down vote have some meaning, or will you continue to make it pointless? – Sheridan Jun 27 '14 at 09:14
  • I downvoted. The OP said "The ViewModel has a property (e.g. ItemsCount) which I'd like to be bound to the Items.Count property of the DataGrid" – Sivasubramanian Jun 27 '14 at 09:20
  • *Really*... you down voted for that??? Sorry, but that's an absolutely pathetic reason. The question author clearly just wants to see the number of items in there collections. – Sheridan Jun 27 '14 at 09:22
  • @Sheridan I assume your last line sould be a get and not set? If that is the case I would understand your example and can imagine how this would solve my problem, if not I have no idea how and when the ItemCount would ever get updated. – THW Jun 27 '14 at 13:04
  • Strange... I could have sworn that I updated it before. Either way, you're correct and it's updated now. – Sheridan Jun 27 '14 at 13:10