Here is the code for my listBox ʻa, as you can when you click on checkBox (isChecked == true, Name = SelectMessage), so that this element is selected in the list and you cannot select multiple elements using CTRL.

<ListBox x:Name="listBoxOfMessages" SelectionMode="Single" ItemsSource="{Binding Messages}" IsSynchronizedWithCurrentItem="True" Margin="0,0,15,0"> <ListBox.ItemTemplate > <DataTemplate > <Grid Name="Grid2" > <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <StackPanel Name="MyStack" Grid.Column="0" > <CheckBox Margin="2" Name="SelectMessage" Click="SelectMessage_Click" /> <CheckBox Name="CircleCheckBox" Click="CircleCheckBox_Click" Style="{StaticResource styleCustomCheckBoxCircle}"/> <CheckBox Name="FlagCheckBox" Click="FlagCheckBox_Click" Style="{StaticResource styleCustomCheckBoxFlag}" /> </StackPanel> <StackPanel Grid.Column="1" > <TextBlock Text="{Binding Author}" FontWeight="Bold" FontSize="13"/> <TextBlock Text="{Binding DateTime}" FontSize="11"/> </StackPanel> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 
  • And what should happen to the checkbox when another list item is selected? - VladD
  • @VladD Stay intact, until the user clicks on it. - Nikita
  • Well, and if it is pressed, and the user clicks on it again, what should happen to the selected list item? - VladD
  • @VladD Become an unselected item in the list. - Nikita
  • What element should be selected? No? - VladD

1 answer 1

You want a little non-standard behavior. Your selection of the selected object is not tied strictly to the state of the data. In principle, there is no problem with this, but you will have to use some code-behind.

We write:

 <ListBox x:Name="listBoxOfMessages" SelectionMode="Single" ItemsSource="{Binding Messages}" IsSynchronizedWithCurrentItem="True" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> <ListBox.ItemTemplate> <DataTemplate> <CheckBox Name="SelectMessage" Click="SelectMessage_Click" Content="{Binding}"/> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

I simplified your XAML a little for an example. Also added HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" to stretch the content to the entire width of the element.

Now, code-behind.

 private void SelectMessage_Click(object sender, RoutedEventArgs e) { var checkBox = (CheckBox)sender; var container = FindParentOfType<ListBoxItem>(checkBox); container.IsSelected = checkBox.IsChecked.Value; } static private T FindParentOfType<T>(FrameworkElement element) where T : FrameworkElement { while (element != null) { if (element is T t) return t; element = (FrameworkElement)VisualTreeHelper.GetParent(element); } return null; } 

We start, it turns out this:

Suddenly, the hunter runs out, and in the Rabbit's ban for 416 days!

  • Swears at the construction of if (element is T t) return t; How to convert it correctly? - Nikita
  • @Nikita: Do you have an old Studio? - VladD
  • 2015, is it greatly affected? - Nikita
  • I solved this problem, thanks for the help. - Nikita
  • @Nikita: Yes, the old version of the language does not support this construct. You need something like this: T t = element as T; if (t != null) return t; T t = element as T; if (t != null) return t; - VladD