Parent i ListBoxItem - Phone 7

0

Witam

Kod xaml:

<ListBox x:Name="LayerVisibilityList" Grid.Row="2" SelectionMode="Multiple" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                 SelectionChanged="LayerVisibilityList_SelectionChanged">

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Width="480">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="80"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="80"/>
                        </Grid.ColumnDefinitions>
                        <Button Grid.Column="0" Content="&lt;" FontSize="14" Click="LayerDown"/>
                        <Grid Grid.Column="1">
                            <TextBlock Text="{Binding LayerID}" FontSize="35" HorizontalAlignment="Center"/>
                        </Grid>
                        <Button Grid.Column="2" Content=">" FontSize="14" Click="LayerUp"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>

Problem polega na tym, że chciałbym po kliknięciu na button w konkretnym itemie uzyskać referencję do tego itema. Próbowałem:

object obj = ((Grid)((Button)sender).Parent).Parent

ale zwraca null'a.

Pzdr

0

Spróbuj użyć tej klasy:

  public static class TreeHelper
        {
            public static T FindParent<T>(UIElement control) where T : UIElement
            {
                UIElement p = VisualTreeHelper.GetParent(control) as UIElement;

                if (p != null)
                {
                    if (p is T)
                        return p as T;
                    else
                        return FindParent<T>(p);
                }

                return null;
            }
        }

Przykład:

private void Button_Click(object sender, RoutedEventArgs e)
        {
            ListBoxItem item = FindParent<ListBoxItem>(sender as UIElement);
            ItemViewModel model = item.Content as ItemViewModel;
        } 

1 użytkowników online, w tym zalogowanych: 0, gości: 1