如何在WPF中的ListBox中访问DataTemplate中的控件

时间:2017-07-31 06:21:18

标签: c# wpf xaml listbox visualtreehelper

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

(当我点击mouseleftbutton“closeAll”时,将其可见性更改为折叠)

我可以使用FindDescendantByName方法执行此操作,但它仅适用于屏幕上的列表框项目(仅前10项)但是当我向下滚动时,我发现这对其他列表框项目不起作用。

我认为由于VisualTreeHelper而发生错误。我可以使用什么代替VisualTreeHelper?

谢谢..

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

1 个答案:

答案 0 :(得分:0)

注意使用visualtreehelper,这是因为列表是虚拟化的,因此只创建了前十个项目,然后由下一个项目替换....并且您放弃了修改

您不得使用代码

处理数据模板中的元素

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

 <StackPanel Grid.Row="1" Name="stackPanelDetay" Visibility="{Binding myBoolean, Converter=BoolToVisibility}">