Всплывающее окно останется открытым, только если я нажму и удержу кнопку мыши.
Вот xaml всплывающего окна:
Код: Выделить всё
Код: Выделить всё
private void EmbodiedCarbonTextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (sender is TextBox textBox)
{
_activeTextBox = textBox; // Store the active TextBox
if (!_isPopupOpen)
{
ValuePopup.PlacementTarget = textBox; // Position the Popup relative to the TextBox
ValuePopup.IsOpen = true; // Open the Popup
_isPopupOpen = true; // Set the flag
}
e.Handled = true; // Prevent the click from propagating further
}
}
private void NumberOnly_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9.-]+");
e.Handled = regex.IsMatch(e.Text);
//if (_isPopupOpen)
// {
// ClosePopup(); // Close the popup when typing starts
//}
}
private void EmbodiedCarbonTextBox_LostFocus(object sender, RoutedEventArgs e)
{
//if (_isPopupOpen)
//{
// ClosePopup(); // Close the popup when focus is lost
//}
}
private void EmbodiedCarbonTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (_isPopupOpen)
{
ClosePopup(); // Close the popup when typing starts
}
}
protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
{
if (_isPopupOpen)
{
// Check if the click is outside the TextBox and the Popup
if (!_activeTextBox.IsMouseOver && !ValuePopup.IsMouseOver)
{
ClosePopup();
}
}
base.OnPreviewMouseDown(e);
}
private void PopupContent_ValueSelected(object sender, string selectedValue)
{
if (_activeTextBox != null)
{
_activeTextBox.Text = selectedValue; // Update the TextBox with the selected value
}
ClosePopup(); // Close the Popup after a selection
}
private void ClosePopup()
{
ValuePopup.IsOpen = false; // Close the Popup
_isPopupOpen = false; // Reset the flag
_activeTextBox = null; // Clear the active TextBox reference
}
}
Код: Выделить всё
Спасибо!>
Подробнее здесь: https://stackoverflow.com/questions/793 ... is-clicked