2012-09-04 42 views
1

我想將我的TextBlock.Text綁定到ListBox.SelectedItems.Count,但我發現當我在列表框中多選項時,我的TextBlock不顯示任何內容。綁定到Windows應用商店的SelectedItems.Count

我記得這種方式在WPF中工作,但它不再適用於Windows Store應用程序。

有沒有其他解決簡單問題的方法?

<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> 

這裏是轉換器,但在這種情況下沒有必要。

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(); 
    } 

} 

我加載一些數據在程序的入口主要。

 List<string> gogoString = new List<string>(); 

     for (int i = 0; i < 4; i++) 
      gogoString.Add(i.ToString()); 

     listBox.ItemsSource = gogoString; 
+2

您是否嘗試從綁定中刪除'Mode =「TwoWay'? – XAMeLi

+0

是的,但它也不起作用,也許Microsoft Metro api團隊將在未來改進此功能嗎? –

+0

」value「如果你在你的轉換器中放置了一個斷點? – mathieu

回答

0

好的,我遲到了,找到答案。

ListBox在WPF中執行INotifyPropertyChanged但在Windows Store應用程序中不執行。這就是爲什麼我無法在Windows應用商店中從ListBox.Items.CountListBox.SelectedItems.Count收到通知。