我在vb.net上为Windows Phone 8制作了一个文本框。
它工作正常,但我想让列表框可点击。
如果你推文,没有任何反应。 有谁可以帮助我吗? 这是我的代码:
的Xaml:
<phone:PhoneApplicationPage
x:Class="PhoneApp1.LijstRestaurants"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">
<Grid x:Name="LayoutRoot" Background="#FF386A06">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel Grid.Row="0" Margin="12,17,0,28"/>
<!--ContentPanel - place additional content here-->
<TextBox x:Name="txtSearch" TextChanged="txtSearch_TextChanged" Text="" Margin="0,48,0,552" Height="80" ></TextBox>
<ListBox x:Name="listBox" FontSize="21" Height="580" Margin="0,100,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Restaurantnaam}" Width="440" Margin="10,15,0,0" Height="80"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock HorizontalAlignment="Left" Margin="12,10,0,0" TextWrapping="Wrap" Text="Zoek je gewenste restaurant" VerticalAlignment="Top" FontSize="36" TextAlignment="Center"/>
</Grid>
</phone:PhoneApplicationPage>
VB.net:
Partial Public Class LijstRestaurants
Inherits PhoneApplicationPage
Private lstRestaurants As List(Of ClassRestaurants) = Nothing
Public Sub New()
InitializeComponent()
End Sub
Protected Overrides Sub OnNavigatedTo(args As NavigationEventArgs)
lstRestaurants = New List(Of ClassRestaurants)()
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "ABDIJ DER TRAPPISTEN" & vbNewLine & "Scheldemolenstraat Haven 1800, 9130 Beveren" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "ASTER-CAMPUS MELVEREN" & vbNewLine & "Antwerpsesteenweg 496, 2390 Malle" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "BAYER LILLO" & vbNewLine & "Retieseweg 111, 2440 Geel" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "CHR PELTZER LA TOURELLE" & vbNewLine & "Sint-jozefstraat 35, 2000 Antwerpen" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "CLINIQUE SAINT-JEAN" & vbNewLine & "Haven 1071 - Kapeldijk 40, 9130 Kallo" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "CPAS NIVILLES" & vbNewLine & "Minderbroedersstraat 5, 3800 Sint-Truiden" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "CUISINE CENTRALE CLINIQUE SAIN" & vbNewLine & "Vaartkaai 2, 2170 Merksem" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "DANONE N.V." & vbNewLine & "Rue Hauzeur de Simony 20, 4800 Verviers" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "ELECTRABEL KALLO" & vbNewLine & "Stiftstrasse 9, 4711 Astenet" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "ELECTRABEL KCD DOEL" & vbNewLine & "Haven 1992 Ketenislaan 2, 9130 Kallo" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "ELIA MERKSEM" & vbNewLine & "Huffengrasse 4/6, 4700 Eupen" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "ENI GAS & POWER" & vbNewLine & "Haven 507 Scheldelaan 420, 2040 Antwerpen" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "EUROPESE COMMISSIE IRMM" & vbNewLine & "Melverencentrum 111, 3800 Sint-Truiden" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "KARMEL BRUGGE" & vbNewLine & "Rue Samiette 70, 1400 Nivilles" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "KATHARINESTIFT" & vbNewLine & "Avenue Leopold Wiener 20, 1170 Brussel" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "LANXESS KALLO" & vbNewLine & "Lindestraat 3, 1540 Herne" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "MEALS ON WHEELS" & vbNewLine & "Sint-Jozefstraat 1A, 9041 Oostakker" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "MICROFIBRES EUROPE N.V." & vbNewLine & "Boulevard du jardin botanique 32, 1000 Brussel" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "MINDERBROEDERS" & vbNewLine & "Boulevard du jardin botanique 32, 1000 Brussel" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "O.C.M.W. HARELBEKE" & vbNewLine & "Lange Meire 56, 9270 Laarne" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "O.C.M.W. NINOVE" & vbNewLine & "Rue Guimard 1, 1040 Brussel" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "O.C.M.W. WIELSBEKE" & vbNewLine & "Hernieuwenstraat 15, 8710 Wielsbeke" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "O.C.M.C. WAREGEM - WZC DE MEERS" & vbNewLine & "Kapelaniestraat 2, 8953 Heuvelland-Wijtschate" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "O.C.M.W. HEUVELLAND ST MEDARD WZC." & vbNewLine & "Schakelstraat 41, 8790 Waregem" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "OUDE ABDIJ VAN DRONGEN V.Z.W." & vbNewLine & "Drongenplein 26, 9031 Drongen" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "RESIDENCE SAINTE-ANE" & vbNewLine & "Centrumlaan 173, 9400 Ninove" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "RUSTHUIS SINT-FELIX" & vbNewLine & "Vrijdomkaai 31, 8530 Harelbeke" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "SANKT-NIKOLAUS HOSPITAL" & vbNewLine & "Stationstraat 170, 3110 Rotselaar" _
})
lstRestaurants.Add(New ClassRestaurants() With { _
.Restaurantnaam = "SINT-JOZEF INSTITUUT" & vbNewLine & "Ezelstraat 28, 8000 Brugge" _
})
Me.listBox.ItemsSource = lstRestaurants
End Sub
Private Sub txtSearch_TextChanged(sender As Object, e As TextChangedEventArgs)
If lstRestaurants IsNot Nothing Then
Me.listBox.ItemsSource = lstRestaurants.Where(Function(w) w.Restaurantnaam.ToUpper().StartsWith(txtSearch.Text.ToUpper()))
End If
End Sub
End Class
感谢。
答案 0 :(得分:0)
您只需订阅ListBox的SelectionChanged事件,而不是摆弄Buttons或Tapped Events,而每次点击它的元素时都会触发该事件。
<ListBox SelectionChanged="ListBox_SelectionChanged">
<ListBox.ItemTemplate>
<!-- ... ->
</ListBox.ItemTemplate>
</ListBox>
在你的代码背后,你可以捕捉到这个事件,并找出你的哪些物品(餐馆)造成的。
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if ((sender as ListBox).SelectedItem != null)
{
// This is your selected item:
ClassRestaurants selectedRestaurant = (sender as ListBox).SelectedItem as ClassRestaurants;
MessageBox.Show(selectedRestaurant.Restaurantnaam, "You selected:", MessageBoxButton.OK);
}
}
很抱歉这是C#代码,但我相信你可以把它翻译成VB.net
希望有所帮助。
答案 1 :(得分:-1)
将Tap事件添加到TextBlock
或者用Button替换TextBlock并添加click事件并将其内容绑定到文本
<ListBox x:Name="listBox" FontSize="21" Height="580" Margin="0,100,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Restaurantnaam}" Width="440" Margin="10,15,0,0" Height="80" Tap="TextBlock_Tap"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox x:Name="listBox" FontSize="21" Height="580" Margin="0,100,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="{Binding Restaurantnaam}" Width="440" Margin="10,15,0,0" Height="80" Click="Button_Click"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
确保代码隐藏文件(PhoneApp1.LijstRestaurants.xaml.cs)中的Textblock_Tap或Button_Click(无论您使用哪个)。下面是C#代码,很容易被修改为vb.net。
private void TextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button b = sender as Button;
// Use datacontext to get associated ClassRestaurants
ClassRestaurants c = (b.DataContext) as ClassRestaurants;
}