Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextBlock Text Binding an ObservableCollection.Count property

I have a ObservableCollection<Sportisti> that starts out with 0 elements (but is initialized to a new object at the creation of the window). I wanted to bind the Count property to a TextBox. Unfortunately, this causes my application to crash whenever I try to open the window in question.

Now, since I have a ListView on the same page, successfully showing the elements of the collection in question, I thought I could simply extract the number of rows from there, but that also lead to a crash.

<TextBox Text="{Binding ElementName=lvTabela, Path=Items.Count}"
         Grid.Row="4" Grid.Column="1" Margin="0,3,60,3"
         DockPanel.Dock="Top" IsReadOnly="True" />

Note that in the .xaml file I can see the content of the TextBox is 0.

Any idea why this is happening?

like image 896
NLuburić Avatar asked May 08 '13 16:05

NLuburić


2 Answers

My mistake, I forgot to add the Mode=OneWay to the binding. The problem was that, even though the TextBox wasn't editable, the system recognized this as a potential way of changing the ListView.ItemCount attribute, which is read-only.

like image 60
NLuburić Avatar answered Nov 15 '22 17:11

NLuburić


My suggestion: Initialize your collection to empty objects in your code when the the list initializes. This may help you.

like image 28
ClickBright Avatar answered Nov 15 '22 16:11

ClickBright