2017-04-03 59 views
0

我有這個ListView在XAML:從ListView控件的ItemTemplate一些文本複製到一個字符串中UWP

<ListView x:Name="listView" Margin="10,72,10,120" Background="#7F000000" BorderBrush="#FF00AEFF" BorderThickness="1"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <StackPanel BorderThickness="0,0,0,1"> 
         <TextBlock Text="{Binding SSID}" Foreground="#FF067EB6" Margin="0,5,0,0"></TextBlock> 
         <TextBlock Text="{Binding BSSID}" Foreground="#FF067EB6"></TextBlock> 
         <TextBlock Text="{Binding NumOfBars}" Foreground="#FF067EB6"></TextBlock> 
         <TextBlock Text="{Binding WpsPin}" Foreground="#FF067EB6" Margin="0,0,0,5"></TextBlock> 
        </StackPanel> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 

而且這樣的事情在後面的代碼:

class Networks 
{ 
    public string SSID { get; set; } 
    public string BSSID { get; set; } 
    public string NumOfBars { get; set; } 
    public string WpsPin { get; set; } 
} 

而這一次使用ListView

listView.ItemsSource = null; 
ObservableCollection<Networks> nets = new ObservableCollection<Networks>(); 
nets.Add(new Networks() { SSID = "SSID: " + networkSSID, BSSID = "BSSID: " + network.Bssid, NumOfBars = "Signal: " + network.SignalBars.ToString() + "/4", WpsPin = "WPS Pin: " + wpspin }); 
listView.ItemsSource = nets; 

現在,我想選擇的項目中的項目內容把它放在一個字符串中。例如,我想獲取所選項目的「WpsPin」值。

我該怎麼辦?

回答

1

您可以訂閱ListView.SelectionChanged事件和類似下面的代碼實現:

XAML:

<ListView ... SelectionChanged="SelectionChanged"> 

C#:

private void SelectionChanged(object sender, SelectionChangedEventArgs e) { 
    Networks network = e.AddedItems.FirstOrDefault() as Networks; 
    if (network == null) return; 

    System.Diagnostics.Debug.WriteLine(network.WpsPin); 
} 
+0

謝謝,對我幫助很大。 – SaeedZeroOne

相關問題