There is no SelectionRetention = RetentionMode.retain
in the official Microsoft ListBox document now. Maybe there's a third tool that can do that, but if you just use ListBox, we need to implement it manually now. I add some parts to your demo code to implement it manually:
- Add a
Flag
property to yourEntry
like: private bool flag=false;
public bool Flag
{
get{ return flag; }
set
{
flag = value;
OnPropertyChanged("Flag");
}
}
2 . Update onSelectionChanged
as:
void onSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
int selectItem = 0;
foreach(Entry item in list.SelectedItems)
{
item.Flag = !item.Flag;
}
foreach (Entry item in list.Items)
{
if(item.Flag==true)
{
selectItem++;
}
}
countBlock.Text = selectItem + "/" + list.Items.Count + " selected";
}
3.Update the Grid as:
<Grid>
<Grid.Style>
<Style TargetType="Grid">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Text,ElementName=txtFlag}" Value="True" >
<Setter Property="Grid.Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition />
<ColumnDefinition Width="70"/>
<ColumnDefinition Width="70"/>
<ColumnDefinition Width="70"/>
<ColumnDefinition Width="70"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Date}"/>
<TextBlock Grid.Column="1" Text="{Binding Particulars}" />
<TextBlock Grid.Column="2" Text="{Binding Debit}" />
<TextBlock Grid.Column="3" Text="{Binding Credit}" />
<TextBlock Name="txtFlag" Grid.Column="5" Text="{Binding Flag,Mode=TwoWay,UpdateSourceTrigger=LostFocus}" Visibility="Hidden"/>
</Grid>
The result picture is:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.