SaeedZeroOne
SaeedZeroOne

Reputation: 29

Copy some text from ListView ItemTemplate to a string in UWP

I have this ListView in XAML:

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

And something like this in the code behind:

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

And this one to use the 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;

Now, I want to get the content of the selected item in the ItemSource and put it in a string. For example, I wanna get the "WpsPin" value of the selected item.

How can I do it?

Upvotes: 0

Views: 231

Answers (1)

DevAttendant
DevAttendant

Reputation: 636

You can subscribe to the ListView.SelectionChanged event and implement it in the code like the following:

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

Upvotes: 1

Related Questions