如何获取所选列表框项的值

时间:2015-11-15 18:54:37

标签: c# windows-phone-8

我正在将数据从xml文件加载到列表框中。这是我的xaml

<ListBox x:Name="lstSearchCategory" FontFamily="Arial Black" 
         VerticalAlignment="Center" Margin="25,69,19,10" Height="264">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel >
                <Image Source="{Binding Image}" Height="100" Width="100" 
                       HorizontalAlignment="Left"></Image>
                <TextBlock HorizontalAlignment="Right" Text="{Binding Name}" 
                           FontSize="30" Foreground="Black" Margin="140,-100,0,0"/>
                <TextBlock Text="{Binding Category}" FontSize="24" 
                           Foreground="Black" Margin="10,-10,0,0"/>
                <TextBlock Text="{Binding Price}" HorizontalAlignment="Right" 
                           Foreground="Red" Margin="300,-25,0,16"/>
                <Rectangle Width="500"  Fill="Black" Height="0.5"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

这很好用。现在我希望当我选择任何列表框项目时,我得到它各自的值,即图像,价格,类别等。我该怎么做?帮助

4 个答案:

答案 0 :(得分:0)

您需要在ListBox事件中获取所选项目并从ListBox获取DataTemplate(如MSDN所示):

Sub Test()

    Dim arrValues() As Variant
    Dim dblTop As Double
    Dim lngFirst As Long
    Dim lngLast As Long
    Dim i As Long
    Dim j As Long
    Dim dblSum As Double
    Dim arrResult() As Variant

    arrValues = Range("A1:ALL1")
    dblTop = arrValues(1, 1)
    lngFirst = 1
    lngLast = 1

    For i = 1 To 1000
        dblSum = 0
        For j = i To 1000
            dblSum = dblSum + arrValues(1, j)
            If dblSum > dblTop Then
                lngFirst = i
                lngLast = j
                dblTop = dblSum
            End If
        Next
    Next

    Debug.Print "Max value: " & dblTop
    Debug.Print "First index: " & lngFirst
    Debug.Print "Last index: " & lngLast
    arrResult() = Array()
    For k = lngFirst To lngLast
        ReDim Preserve arrResult(UBound(arrResult) + 1)
        arrResult(UBound(arrResult)) = arrValues(1, k)
    Next
    Debug.Print "Sequence: " & Join(arrResult, " + ")

End Sub

答案 1 :(得分:0)

您需要在XAML文件和Tap = "lstSearchCategory_Tap"文件中的代码

下生成.cs
private void lstSearchCategory_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    try
    {
        ListBox ListBoxSelecteditem = (ListBox)sender;
        YourModel model = (YourModel)ListBoxSelecteditem.SelectedItem;            

        string name = model.Name;
        string cat = model.Category;
        .......

        string ControlName = ((System.Windows.FrameworkElement)
                             (((System.Windows.RoutedEventArgs)(e)).OriginalSource)).Name;
        if (ControlName.ToLower() != "name".ToLower())
        {
        }
    }
    catch (Exception ex)
    { }
}   

答案 2 :(得分:0)

试试这个

Error: Unexpected request: GET http://testsite.com/movies/undefined
        Expected GET http://testsite.com/movies/undefined? 
            at $httpBackend (C:/wrokingdir2015/public/lib/angular-mocks/angular-mocks.js:1245)
            at sendReq (C:/wrokingdir2015/public/lib/angular-mocks/public/lib/angular/angular.js:9695)

而不是点击事件添加此

<ListBox Tap="lstSearchCategory_Tap" x:Name="lstSearchCategory">

此处class-name是您绑定名称,价格等值的类的名称

答案 3 :(得分:-1)

如果您通过绑定填充ListBox,则视图模型中应该有一些属性lile SelectedItem。因此,当前所选项应始终存储在viewmodel中以便于访问。只需在viewmodel中为SelectedItem添加绑定,并且每件事都应该有效。