从LongListSelector项获取值

时间:2014-11-11 21:33:36

标签: c# xaml

我用这个模板填充我的LongListSelector:

    <DataTemplate x:Key="LongListSelectorItemTemplate">
        <StackPanel Orientation="Horizontal" Margin="4,4">
            <Grid Tap="Grid_Tap" x:Uid="{Binding Id}">
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Image Grid.RowSpan="3" Width="150" Height="60" Source="{Binding Logo}" VerticalAlignment="Center"  Margin="0,0,15,0"/>
                <TextBlock Grid.Column="1"  Text="{Binding NazwaSklepu}" FontFamily="/Okazjum;component/Fonts/Fonts.zip#Open Sans" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="Black" FontSize="32"/>
                <Image Grid.Column="1" Grid.Row="1" Source="1.0_Images/Vector Smart Object.png" VerticalAlignment="Center" HorizontalAlignment="Left" />
                <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding GodzinyOtwarcia}" FontFamily="/Okazjum;component/Fonts/Fonts.zip#Open Sans Light" VerticalAlignment="Center" HorizontalAlignment="Left" FontSize="14" Foreground="Gray" Margin="25,0,0,0" />
                <Image Grid.Column="1" Grid.Row="2" Source="1.0_Images/Vector Smart Object copy 3.png" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                <TextBlock Grid.Column="1" Grid.Row="2" Text="{Binding Adres}" FontFamily="/Okazjum;component/Fonts/Fonts.zip#Open Sans Light" VerticalAlignment="Center" HorizontalAlignment="Stretch" FontSize="14" Foreground="Gray" Margin="25,0,0,0" x:Name="txtAdres"/>

            </Grid>
            </StackPanel>
    </DataTemplate>

如您所见,我将Tap事件添加到我的网格和Binding id。

如何在C#事件代码中获取此ID?

1 个答案:

答案 0 :(得分:0)

我找到了临时的方式。我把Id放在Tag而不是x:Uid中 并使用以下代码:

 private void Grid_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        int id = -1;
        var element = (FrameworkElement)sender;
        if (int.TryParse((Grid)element.Tag + "", out id)) {
        ... my code 
        }
    }