I want to bind my TextBlock.Text to ListBox.SelectedItems.Count, but I find that when I multi-select items in my listBox, my TextBlock doesn’t show anything.
I remember this way works in WPF, but it doesn’t work in Windows Store App anymore.
Is there any alternative way to solve the simple problem ?
<StackPanel>
<StackPanel.Resources>
<local:NumberToTextConverter x:Key="NumToText" />
</StackPanel.Resources>
<TextBlock Text="{Binding SelectedItems.Count, ElementName=listBox, Mode=TwoWay, Converter={StaticResource NumToText}}"
Height="80" />
<ListBox x:Name="listBox"
SelectionMode="Multiple" />
</StackPanel>
Here is the converter, but it’s not necessary in this case.
internal class NumberToTextConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string culture)
{
if(value != null)
return ((int)value).ToString();
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, string culture)
{
throw new NotImplementedException();
}
}
I load some data at the entry main of the program.
List<string> gogoString = new List<string>();
for (int i = 0; i < 4; i++)
gogoString.Add(i.ToString());
listBox.ItemsSource = gogoString;
Ok, I am late to find the answer.
The
ListBoximplementsINotifyPropertyChangedin WPF but not in Windows Store app. That’s why I can’t get notified fromListBox.Items.CountorListBox.SelectedItems.Countin Windows Store app.