2013-05-01 25 views
1

我想從一個列表框,看起來像這樣我的Windows 8 Store應用內獲得的SelectedItem:獲取的SelectedItem從Control

<ListBox x:Name="listBox" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" Foreground="Black" BorderThickness="0" Background="#FFD8D8D8" /> 

的問題是,列表框不火的SelectedItem propertie。我必須使用IsSynchronizedWithCurrentItem =「True」但出現一個錯誤,表示true不支持此屬性。我需要做什麼或者是否有其他方法來獲得SelectedItem特性?

我有這樣的代碼背後:

namespace ExampleApp 
{ 
    public sealed partial class MainPage : Page, INotifyPropertyChanged 
    { 
     private object currentItem; 

     //Constructor and so on 

     public object SelectedItem 
     { 
      get { Debug.WriteLine("get"); return currentItem; } 
      set { Debug.WriteLine("set"); currentItem = value; NotifyPropertyChanged(); } 
     } 
    } 
} 
+0

【如何通過列表框將selectedItem作爲一個按鈕命令參數?(HTTP的可能重複://計算器.com/questions/18257516/how-to-pass-listbox-selecteditem-as-command-in-a-button) – RubberDuck 2016-06-10 19:14:45

回答

4

你應該試試這個

<ListBox x:Name="listBox" SelectedItem="{Binding ElementName=YourPageName,path=DataContext.SelectedItem, Mode=TwoWay}" Foreground="Black" BorderThickness="0" Background="#FFD8D8D8" /> 
+0

Thank you it works great。我不得不刪除DataContext和SelectedItem,現在它工作得很好。 – Cilenco 2013-05-01 08:28:33

+0

哇。謝謝。我猜想微軟沒有在SelectedItem上實現合理的OneWayToSource綁定是有原因的,但我想不出來。 – George 2015-01-21 20:01:23

相關問題