2017-08-04 19 views
1

私の状況では、productsLocLB.items.Clear()を使用しています。値だけを削除するのではなく、すべての項目を削除します。WPFのリストアイテムを削除せずにListBoxから値を消去する方法

ListBox x:Name="productsLocLB" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="False" BorderThickness="0" ItemsPanel="{StaticResource ListboxItemPanel}" HorizontalAlignment="Stretch" Width="Auto" Background="{x:Null}" Margin="0,25,0,0" Height="564" VerticalAlignment="Top" ItemContainerStyle="{StaticResource RackListBoxItemStyle}" > 
    <ListBox.ItemTemplate> 
     <!--User Info Cadview item Tamplate --> 
     <DataTemplate> 
      <Border > 
       <Grid HorizontalAlignment="Left" Width="150" Height="60" VerticalAlignment="Top" Cursor="Hand" MouseLeftButtonDown="Grid_OnMouseDownOrTouchDown" TouchDown="Grid_OnMouseDownOrTouchDown"> 
        <Image x:Name="productLocImg" Width="150" Source="{Binding SPImage}" Stretch="Fill"/> 
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> 
         <Label Content="{Binding ProductName}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontSize="18" Background="#99000000"/> 
         <Label Content="{Binding Avalability}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Black" FontSize="14" Background="#90FFFFFF" FontWeight="Bold"/> 
        </StackPanel> 
       </Grid> 
      </Border> 
     </DataTemplate> 

     <!----> 
    </ListBox.ItemTemplate> 
<ListItem></ListItem> 
<ListItem></ListItem> 
<ListItem></ListItem> 

</ListBox> 
+0

あなたの 'ItemsSource'は' Binding'によって設定されていますか? –

+0

はいそれは@MightyBadaboomです –

+0

そして、(例えば) 'ProductName = string.Empty'だけをすべてのエントリに設定したいですか? –

答えて

1

productsLocLB.items.Clear() 

を削除し、ItemsSourceにバインドされているあなたのコレクションを想定した(代わりに書き込みMyItemsと呼ばれ、あなたは、このコレクション内の各エントリに対してProductName = string.Empty;を設定したい。

foreach(var item in MyItems) 
{ 
    ProductName = string.Empty; 
} 

INotifyPropertyChangedも実装する必要があります。そうしないと、UIは通知されず、新しい値は表示されません。

関連する問題