[英]How to Clear values from ListBox without removing List items in WPF
In my situation When I use productsLocLB.items.Clear(). 在我的情况下,当我使用productsLocLB.items.Clear()时。 it removes All items instead of removing only values. 它删除所有项目,而不是仅删除值。
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>
Remove 去掉
productsLocLB.items.Clear()
and write instead (assuming your collection which is bound to the ItemsSource
is called MyItems
and you want to set ProductName = string.Empty;
for each entry in this collection. 然后写(假设您绑定到ItemsSource
集合称为MyItems
并且您想为此集合中的每个条目设置ProductName = string.Empty;
MyItems
foreach(var item in MyItems)
{
ProductName = string.Empty;
}
You have to implement INotifyPropertyChanged
as well or the UI won't be notized and the new values won't shown. 您还必须实现INotifyPropertyChanged
,否则将不会通知UI且不会显示新值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.