2017-08-04 57 views
1

在我的情況下當我使用productsLocLB.items.Clear()時。它將刪除所有項目而不是僅刪除值。如何清除ListBox中的值而不刪除WPF中的列表項

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,否則用戶界面將不會被標註,並且新值將不會顯示。