Get Bounded item from ListView : ListView « Windows Presentation Foundation « VB.Net






Get Bounded item from ListView

Get Bounded item from ListView
     
<Window x:Class="WpfApplication1.Window1"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Window.Resources>
    <XmlDataProvider x:Key="MyData" XPath="/Info">
      <x:XData>
        <Info xmlns="">
          <Song Name="Song 1" Artist="Artist 1"/>
          <Song Name="Song 2" Artist="Artist 2"/>
          <Song Name="Song 3" Artist="Artist 1"/>
        </Info>
      </x:XData>
    </XmlDataProvider>
    <Style x:Key="MyContainer" TargetType="{x:Type ListViewItem}">
      <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="true">
          <Setter Property="Cursor" Value="Hand"/>
        </Trigger>
        <MultiTrigger>
          <MultiTrigger.Conditions>
            <Condition Property="IsSelected" Value="true" />
            <Condition Property="Selector.IsSelectionActive" Value="true" />
          </MultiTrigger.Conditions>
          <Setter Property="Foreground" Value="Red" />
        </MultiTrigger>
      </Style.Triggers>
    </Style>
    <DataTemplate x:Key="FirstCell">
      <StackPanel Orientation="Horizontal">
        <CheckBox IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"/>
      </StackPanel>
    </DataTemplate>
  </Window.Resources>
  <StackPanel>
    <ListView ItemsSource="{Binding Source={StaticResource MyData}, XPath=Song}" 
           ItemContainerStyle="{StaticResource MyContainer}" 
           SelectionChanged="mySelectionChanged"
           SelectionMode="Single" 
           Name="myPlaylist">
      <ListView.View>
        <GridView>
          <GridViewColumn CellTemplate="{StaticResource FirstCell}" Width="30"/>
          <GridViewColumn Header="Name" DisplayMemberBinding="{Binding XPath=@Name}" Width="80"/>
          <GridViewColumn Header="Artist"  DisplayMemberBinding="{Binding XPath=@Artist}" Width="80" />
        </GridView>
      </ListView.View>
    </ListView>
    <TextBlock Margin="20" Name="NowPlaying"/>
  </StackPanel>
</Window>

//File:Window.xaml.vb

Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Media
Imports System.Windows.Shapes
Imports System.Windows.Controls.Primitives
Imports System.Collections.ObjectModel
Imports System.Xml


Namespace WpfApplication1
  Public Partial Class Window1
    Inherits Window
    Private Sub mySelectionChanged(sender As Object, e As SelectionChangedEventArgs)
      Dim mySelectedElement As XmlElement = DirectCast(myPlaylist.SelectedItem, XmlElement)
      NowPlaying.Text = mySelectedElement.GetAttribute("Name").ToString() & " by " & mySelectedElement.GetAttribute("Artist").ToString()
    End Sub
  End Class
End Namespace

   
    
    
    
    
  








Related examples in the same category

1.ListView control with controls for columnsListView control with controls for columns
2.Set Binding ListView.ItemsSourceProperty to ListViewSet Binding ListView.ItemsSourceProperty to ListView
3.Create a ListView control that uses a GridView view mode to display a collection of DateTime objects.Create a ListView control that uses a GridView view mode to display a collection of DateTime objects.
4.Create Binding for ListView in codeCreate Binding for ListView in code
5.Create a ListView control that uses a GridView view mode to display the contents of an ObservableCollection<(Of <(T>)>).Create a ListView control that uses a GridView view mode to display the contents of an ObservableCollection<(Of <(T>)>).
6.Use three TextBlocks in one ListViewItemUse three TextBlocks in one ListViewItem
7.Create a ListView control that uses a GridView view mode to display dates.Create a ListView control that uses a GridView view mode to display dates.