1

I have a ListBox including an ItemTemplate with a StackPanel. I want to access that stackpanel and change its visibility.

(Change it's visibility to collapsed when I click mouseleftbutton "closeAll")

I can do that with FindDescendantByName Method but it works for only listbox items on screen (Only first 10 items) but when I am scrolling down, I see that this is not working for other listbox items.

I think that errors occurs because of VisualTreeHelper. What can I use instead of VisualTreeHelper?

Thanks..

XAML CODE

<ListBox x:Name="listBoxEditPast"  SelectionMode="Single"  Margin="0" Background="#272B34" ScrollViewer.VerticalScrollBarVisibility="Visible">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Border Grid.Row="0"   BorderThickness="4,0,0,0" Margin="2,0,0,0" Height="29"  Background="#2E323B" Width="1050" BorderBrush="#1373A9" MouseLeftButtonDown="Border_MouseLeftButtonDown">
                    <DockPanel Name="dockPanelPast" Margin="0,4,0,0">
                        <Image Name="imgArrow" Source="images/down-arrow.png" HorizontalAlignment="Left" Width="20" Height="18"/>
                        <TextBlock Text="{Binding CreateDate}" Name="txtTarih" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="16"/>
                        <TextBlock Text="{Binding SarjNo}" Name="txtSarjNo" Foreground="#FF9CA518" HorizontalAlignment="Stretch" VerticalAlignment="Center" FontSize="16" Margin="50,0,0,0" Width="90"/>
                        <TextBlock Text="{Binding Adi}" Name="txtReceteAdi" Foreground="#FF26A053"  VerticalAlignment="Center" FontSize="16" Margin="40,0,0,0" HorizontalAlignment="Stretch"/>
                         <Button Content="Detaylar" Style="{StaticResource BlueButton}" HorizontalAlignment="Right" VerticalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" DockPanel.Dock="Right"/>
                    </DockPanel>
                </Border>
                <StackPanel Grid.Row="1"  Name="stackPanelDetay" Tag="{Binding ID}">
                    <DockPanel>
                        <TextBlock Text="Sipariş No" Foreground="#D9480F" VerticalAlignment="Center" />
                        <TextBlock Text="Parça" Foreground="#AF0FD9" VerticalAlignment="Center" Margin="50,0,0,0" Width="200" />
                        <TextBlock Text="Malzeme" Foreground="White" VerticalAlignment="Center" Margin="150,0,0,0" Width="90"/>
                        <TextBlock Text="Müşteri" Foreground="#AF0FD9" VerticalAlignment="Center" Margin="70,0,0,0" />
                    </DockPanel>
                    <DockPanel>
                        <TextBlock Text="{Binding ID}" Foreground="White"  VerticalAlignment="Center" Width="100"/>
                        <TextBlock Text="{Binding ParcaKoduAdi}" Foreground="White"  VerticalAlignment="Center" Margin="5,0,0,0" Width="200"  />
                        <TextBlock Text="{Binding Malzeme}" Foreground="White"  VerticalAlignment="Center" Margin="152,0,0,0" Width="90" />
                        <TextBlock Text="{Binding MusteriKoduAdi}" Foreground="White"  VerticalAlignment="Center" Margin="70,0,0,0" />
                    </DockPanel>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

C# CODE

public static class FrameworkElementExtensions
{
    public static FrameworkElement FindDescendantByName(this FrameworkElement element, string name)
    {
        if (element == null || string.IsNullOrWhiteSpace(name)) { return null; }

        if (name.Equals(element.Name, StringComparison.OrdinalIgnoreCase))
        {
            return element;
        }
        var childCount = VisualTreeHelper.GetChildrenCount(element);
        for (int i = 0; i < childCount; i++)
        {
            var result = (VisualTreeHelper.GetChild(element, i) as FrameworkElement).FindDescendantByName(name);
            if (result != null) { return result; }
        }
        return null;
    }
}

private void closeAll_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    // StackPanel panel = LayoutHelper.FindElement(listBoxEditPast, n => n.GetType() == typeof(StackPanel)) as StackPanel;

    for (int i = 0; i < listBoxEditPast.Items.Count; i++)
    {
        var element = listBoxEditPast.ItemContainerGenerator.ContainerFromIndex(i) as FrameworkElement;
        if (element != null)
        {
            var sp = element.FindDescendantByName("stackPanelDetay") as StackPanel;
            if (sp != null)
            {
                sp.Visibility = Visibility.Collapsed;
            }
        }
    }
}
Nikolaj Dam Larsen
  • 5,455
  • 4
  • 32
  • 45
Barsblue
  • 109
  • 1
  • 15

1 Answers1

0

noting to do with the visualtreehelper, this is because the list is virtualized, so only the first ten items are created and then replaced by the ten next....and you loose your modifications

you must not work with the element in the data template by code

iterate through your data to set a boolean to true/false for all and then change the stack and bind the visibility to this boolean

 <StackPanel Grid.Row="1" Name="stackPanelDetay" Visibility="{Binding myBoolean, Converter=BoolToVisibility}">
GCamel
  • 612
  • 4
  • 8