I have a comboBox. When opened it shows checkboxes.
Problem: While checking/unchecking one becomes selected item for my comboBox. I don't want that.
I tried:
<ComboBox Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top"
Name="flagComboBox" IsTextSearchEnabled="True" Margin="0,10,0,0"
IsEditable="False" Text="-- Select flags -- (does not show)"
>
<ComboBox.ItemTemplate>
<DataTemplate >
<CheckBox Name="checkFileException"
Checked="Check_Flag" Unchecked="Uncheck_Flag"
Content="{Binding}"
CommandParameter="{Binding}"
>
</CheckBox>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
You can do this:
In XAML, first, change
Text="-- Select flags -- (does not show)"
with
PlaceholderText="-- Select flags --"
Then, add an event when the user change selection in the ComboBox
SelectionChanged="ComboBox_SelectionChanged"
In your code, add:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
flagComboBox.SelectedIndex = -1;
}
The user will see the PlaceholderText in the ComboBox
EDIT: If you want to keep open the ComboBox until the user press anywhere else on the screen (so the user will continue to see the ComboBox even if clicks on the text) you could do a workaround:
In your XAML file, add three events to the ComboBox:
GotFocus="flagComboBox_GotFocus" LostFocus="flagComboBox_LostFocus" DropDownClosed="flagComboBox_DropDownClosed"
In code, create a new bool
that indicates if the user is using the ComboBox
public sealed partial class MainPage : Page
{
bool isPointerOnComboBox = false; // <-- add the bool here
public MainPage()
Then, add these three voids
private void flagComboBox_DropDownClosed(object sender, object e)
{
if (isPointerOnComboBox.Equals(true))
{
flagComboBox.IsDropDownOpen = true;
}
}
private void flagComboBox_GotFocus(object sender, RoutedEventArgs e)
{
isPointerOnComboBox = true;
}
private void flagComboBox_LostFocus(object sender, RoutedEventArgs e)
{
isPointerOnComboBox = false;
}
If the ComboBox got focus (it was selected by the user), the ComboBox will reopen automatically. If the ComboBox hasn't got focus (the user clicked somewhere else), the ComboBox won't reopen.