1

我有一个 ListBox,包括一个带有 StackPanel 的 ItemTemplate。我想访问该堆栈面板并更改其可见性。

(当我单击鼠标左键“closeAll”时将其可见性更改为折叠)

我可以使用 FindDescendantByName 方法做到这一点,但它仅适用于屏幕上的列表框项目(仅前 10 个项目),但是当我向下滚动时,我发现这不适用于其他列表框项目。

我认为发生错误是因为 VisualTreeHelper。我可以使用什么来代替 VisualTreeHelper?

谢谢..

XAML 代码

<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# 代码

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;
            }
        }
    }
}
4

1 回答 1

0

注意与visualtreehelper有关,这是因为列表是虚拟化的,所以只有前十个项目被创建,然后被接下来的十个替换......你失去了你的修改

您不能通过代码使用数据模板中的元素

遍历您的数据以将所有布尔值设置为 true/false,然后更改堆栈并将可见性绑定到该布尔值

 <StackPanel Grid.Row="1" Name="stackPanelDetay" Visibility="{Binding myBoolean, Converter=BoolToVisibility}">
于 2017-07-31T11:29:31.210 回答