I'm trying to figure out what I need to change to bind this code to a listview.
XAML:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Tracks" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="26" />
<RowDefinition Height="265*" />
</Grid.RowDefinitions>
<Menu Name="Menu1" />
<Frame Grid.Row="2" Name="Frame1" Source="PageSearchResults.xaml" />
<StackPanel Orientation="Horizontal" Grid.Row="1">
<Button Name="AddSite">+</Button>
<ListView Name="ListView1" MouseDoubleClick="ListViewItem_MouseDoubleClick">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Name="mnuDelete" Header="Delete" />
</ContextMenu>
</ListView.ContextMenu>
</ListView>
</StackPanel>
</Grid>
</Window>
VB:
Class MainWindow
Dim bookmarks As New ArrayList
Private Sub mnuDelete_click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles mnuDelete.Click
If Not ListView1.SelectedValue Is Nothing Then
bookmarks.RemoveAt(ListView1.SelectedValue)
End If
ListView1.Items.RemoveAt(ListView1.SelectedIndex)
End Sub
Private Sub AddSite_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles AddSite.Click
Dim i As Integer = 0
Dim itmX As Integer
Dim itm As New ListViewItem
i = bookmarks.Add(Frame1.Content)
itmX = ListView1.Items.Add(New DictionaryEntry(i, Frame1.Content.title))
ListView1.DisplayMemberPath = "Value"
ListView1.SelectedValuePath = "Key"
End Sub
Private Sub ListViewItem_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
If Not ListView1.SelectedValue Is Nothing Then
Frame1.Content = bookmarks(ListView1.SelectedValue)
End If
End Sub
End Class
The main point is that I want the listbox items to go sideways, sorta like an icon view, or more specifically the Favorites bookmark bar in IE or Firefox. I need to learn how to use binding, and this seems like a good place to start.
Here's the changes that store the pages into the new ObservableCollection:
Class MainWindow
Dim bookmarks As New System.Collections.ObjectModel.ObservableCollection(Of Bookmarks)
Private Sub mnuDelete_click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles mnuDelete.Click
If Not ListView1.SelectedValue Is Nothing Then
bookmarks.RemoveAt(ListView1.SelectedValue)
End If
ListView1.Items.RemoveAt(ListView1.SelectedIndex)
End Sub
Private Sub AddSite_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles AddSite.Click
Dim i As Integer = 0
bookmarks.Add(New Bookmarks(i, Frame1.Content.Title, Frame1.Content))
End Sub
Private Sub ListViewItem_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
If Not ListView1.SelectedValue Is Nothing Then
Frame1.Content =开发者_如何转开发 bookmarks(ListView1.SelectedValue)
Else
Frame1.Navigate(New PageCustomerHome())
End If
End Sub
Private Sub mnuAdvancedSearch_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Frame1.Navigate(New PageAdvancedSearch())
End Sub
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
ListView1.ItemsSource = bookmarks
End Sub
End Class
And the bookmarks class
Public Class Bookmarks
Public Sub New(ByVal key As Integer, ByVal title As String, ByVal page As Page)
Me._key = key
Me._title = title
Me._page = page
End Sub
Public Property Key() As Integer
Get
Return Me._key
End Get
Set(ByVal value As Integer)
Me._key = value
End Set
End Property
Public Property Title() As String
Get
Return Me._title
End Get
Set(ByVal value As String)
Me._title = value
End Set
End Property
Public Property Page() As Page
Get
Return Me._page
End Get
Set(ByVal value As Page)
Me._page = value
End Set
End Property
Private _key As Integer
Private _title As String
Private _page As Page
End Class
Now I'm just not sure what I need to do to change my XAML to Bind to my data.
You can start with something like
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}" />
</StackPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
solution ended up being to set an ItemTemplate and doing the binding there:
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" Margin="3,0,3,0" />
</DataTemplate>
</ListView.ItemTemplate>
精彩评论