How do stop Lost_focus on click of ComboBox in WPF?

Mojtaba_Hakim 281 Reputation points
2022-02-17T13:13:28.26+00:00

I use C# WPF

What I need :

When focus lost on ComboBox , check if ComboBox has no a valid item Show a Error .

My Problem :

In this case when I click on the ComboBox the LostFocus event will fire

but it seems didn't happened , in google search I found this :

What I did :

MyXAML :

 <ComboBox x:Name="SecoundCombo" HorizontalAlignment="Left" Margin="95,105,0,0" VerticalAlignment="Top" Width="359" LostFocus="SecoundCombo_LostFocus" IsEditable="True">
        <ComboBoxItem Content="Jack" HorizontalAlignment="Left" Height="18" VerticalAlignment="Top" Width="402"/>
        <ComboBoxItem Content="Sam" HorizontalAlignment="Left" Height="18" VerticalAlignment="Top" Width="402"/>
        <ComboBoxItem Content="John" HorizontalAlignment="Left" Height="18" VerticalAlignment="Top" Width="402"/>
    </ComboBox>

My Code Behind :

   namespace TEST
{
    public partial class MainWindow : Window
    {
        public static bool ComboisChoosedItem_Valid(ComboBox CMB)
        {
            bool st = true;
            if (!string.IsNullOrEmpty(CMB.Text.Trim()) && CMB.SelectedIndex > -1 && CMB.SelectedItem != null)
            {
                if (CMB.Items.OfType<ComboBoxItem>().Any(cbi => cbi.Content.Equals(CMB.Text)))
                {
                    //ComboBox is Not Empty is Correct Item Selected
                    st = true;
                }
            }
            else
            {
                //ComboBox Is Empty Or Not Correct Item Selected
                st = false; 
            }
            return st;
        }
        public MainWindow()
        {
            InitializeComponent();
        }


        private void SecoundCombo_LostFocus(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource != sender) return;
            if (ComboisChoosedItem_Valid(SecoundCombo) == false)
            {
                MessageBox.Show("Please choose valid item from list");
                SecoundCombo.IsDropDownOpen = true;
            }
        }      
    }
}

What have I tried for LostFocus Problem :

 var textBox = SecoundCombo.Template.FindName("PART_EditableTextBox", SecoundCombo) as TextBox;
        if (textBox.IsFocused != true)
        {
            if (ComboisChoosedItem_Valid(SecoundCombo) == false)
            {
                MessageBox.Show("Please Choose an item");
                SecoundCombo.IsDropDownOpen = true;
            }
        }

summry : Why Lost Focus Happened when did not happen because I was focused on something else and I clicked on the CombBox for First time

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,679 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,299 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
767 questions
{count} votes