2017-07-31 3 views
1

StackPanelを持つItemTemplateを含むListBoxがあります。そのスタックパネルにアクセスしてその可視性を変更したいと思います。WPFのListBoxでDataTemplateのコントロールにアクセスする方法

(私はmouseleftbutton「にcloseAll」をクリックしたときに、それが崩壊し、視認性のchange)

私は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#のコードvisualtreehelperを行うには注目

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

データ項目クラスの別のプロパティに可視性をバインドするだけの理由はありますか? – Clemens

答えて

0

おかげで、これはこれだけ、です最初の10個のアイテムが作成され、次に10個のアイテムに置き換えられます。 ifications

あなたはすべてのための真/ falseにブール値を設定し、スタックを変更し、このブール値

に可視性をバインドするために、あなたのデータによってコード

反復することにより、データテンプレートの要素では動作してはなりません

<StackPanel Grid.Row="1" Name="stackPanelDetay" Visibility="{Binding myBoolean, Converter=BoolToVisibility}"> 
関連する問題