<ListBox
x:Name="listBox"
SelectionChanged="listBox_SelectionChanged">
</ListBox>
void listBox_SelectionChanged(object sender, RoutedEventArgs e) {
// This method is only called when a selection is changed.
}
However, I need to call a function that executes even if the already selected item in the list box is clicked again (which would make it selected had it not already been).
I tried the Selected event handler but received this error in visual studio:
Error CS1061 'ListBox' does not contain a definition for 'Selected' and no accessible extension method 'Selected' accepting a first argument of type 'ListBox' could be found (are you missing a using directive or an assembly reference?)
Is there any other event I might be missing?
Thank you.
CodePudding user response:
- In
.xaml
(assume ListBox item isGrid
)
<ListBox
x:Name="listBox"
SelectionChanged="listBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid PreviewMouseDown="listBox_OnPreviewMouseDown">
<!-- Content -->
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
- In
.xaml.cs
(assumeMyListItemType
is the type of the item inListBox
)
private void listBox_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
var clickedItem = (sender as FrameworkElement)?.DataContext as MyListItemType;
if (clickedItem != listBox.SelectedItem) return;
// same item is selected
// ..
}
private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var clickedItem = (sender as FrameworkElement)?.DataContext as MyListItemType;
// new item is selected
// ..
}
CodePudding user response:
First Example:
<UniformGrid>
<ListBox>
<ListBox.ItemsSource>
<sys:String>QWERTY</sys:String>
</ListBox.ItemsSource>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<EventSetter Event="PreviewMouseLeftButtonDown"
Handler="OnItemClick"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<TextBlock x:Name="tBlock"/>
</UniformGrid>
</Window>
private int count = 0;
private void OnItemClick(object sender, MouseButtonEventArgs e)
{
count ;
tBlock.Text = $"{count}: {((ListBoxItem)sender).Content}{Environment.NewLine}";
}
Second Example:
<ListBox SelectionChanged="OnSelection"
HorizontalContentAlignment="Stretch">
<ListBox.ItemsSource>
<sys:String>QWERTY</sys:String>
</ListBox.ItemsSource>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type sys:Char}">
<Grid>
<TextBlock x:Name="PART_TBlock" Text="{Binding}"/>
<Button x:Name="PART_Btn" Content="{Binding}" Click="OnReselection"
BorderThickness="0" Background="Transparent"
HorizontalContentAlignment="Left"
Visibility="Collapsed"/>
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"
Value="True">
<Setter TargetName="PART_TBlock" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="PART_Btn" Property="Visibility" Value="Visible"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock x:Name="tBlockReselection"/>
</UniformGrid>
</Window>
private void OnSelection(object sender, SelectionChangedEventArgs e)
{
tBlockReselection.Text = $"Selected: {((ListBox)sender).SelectedItem}{Environment.NewLine}";
}
private void OnReselection(object sender, RoutedEventArgs e)
{
tBlockReselection.Text = $"Reselected: {((Button)sender).Content}{Environment.NewLine}";
}
Third Example:
<ItemsControl HorizontalContentAlignment="Stretch">
<ItemsControl.ItemsSource>
<sys:String>QWERTY</sys:String>
</ItemsControl.ItemsSource>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type sys:Char}">
<Button Content="{Binding}" Click="OnClickselection"
HorizontalContentAlignment="Left"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock x:Name="tBlockClickSelection"/>
</UniformGrid>
</Window>
private void OnClickselection(object sender, RoutedEventArgs e)
{
tBlockClickSelection.Text = $"ClickSelection: {((Button)sender).Content}{Environment.NewLine}";
}