Home > Blockchain >  WinUI3 ComboBox how to cancel the selected item
WinUI3 ComboBox how to cancel the selected item

Time:11-06

When I clear the Text of the ComboBox or click again, the selected item is still not canceled.

<ComboBox
    MinWidth="120"
    IsEditable="True"
    PlaceholderText="please select">
    <ComboBoxItem>A</ComboBoxItem>
    <ComboBoxItem>B</ComboBoxItem>
    <ComboBoxItem>C</ComboBoxItem>
</ComboBox>

CodePudding user response:

Try this way:

MainPage.xaml

<Page
    x:Class="ComboBoxes.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ComboBoxes"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <StackPanel>
        <ComboBox
            x:Name="ComboBoxControl"
            MinWidth="120"
            IsEditable="True"
            PlaceholderText="please select">
            <ComboBoxItem Tapped="ComboBoxItem_Tapped">A</ComboBoxItem>
            <ComboBoxItem Tapped="ComboBoxItem_Tapped">B</ComboBoxItem>
            <ComboBoxItem Tapped="ComboBoxItem_Tapped">C</ComboBoxItem>
        </ComboBox>
    </StackPanel>
</Page>

MainPage.xaml.cs

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Input;
using System.Linq;

namespace ComboBoxes;

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        InitializeComponent();
        Loaded  = MainPage_Loaded;
    }

    private object? SelectedItemWhenPopupOpened { get; set; }

    private void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        if (this.ComboBoxControl.FindChildrenOfType<Popup>()?.FirstOrDefault() is Popup popup)
        {
            popup.Opened  = Popup_Opened;
        }
    }

    private void Popup_Opened(object? sender, object e)
    {
        SelectedItemWhenPopupOpened = this.ComboBoxControl.SelectedItem;
    }

    private void ComboBoxItem_Tapped(object sender, TappedRoutedEventArgs e)
    {
        if (sender.Equals(SelectedItemWhenPopupOpened) is true)
        {
            this.ComboBoxControl.SelectedIndex = -1;
            this.ComboBoxControl.Text = "";
        }
    }
}

Extensions.cs

using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml;
using System.Collections.Generic;

namespace ComboBoxes;

public static class Extensions
{
    public static IEnumerable<T> FindChildrenOfType<T>(this DependencyObject parent) where T : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i  )
        {
            DependencyObject child = VisualTreeHelper.GetChild(parent, i);

            if (child is T childOfT)
            {
                yield return childOfT;
            }

            foreach (T grandChild in child.FindChildrenOfType<T>())
            {
                yield return grandChild;
            }
        }

        if (parent is ContentControl contentControl)
        {
            if (contentControl.Content is T contentOfT)
            {
                yield return contentOfT;
            }

            if (contentControl.Content is DependencyObject dependencyObjectContent)
            {
                foreach (T grandChild in dependencyObjectContent.FindChildrenOfType<T>())
                {
                    yield return grandChild;
                }
            }
        }
    }
}

CodePudding user response:

MainPage.xaml.cs

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Input;
using System.Linq;

namespace ComboBoxes;

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        InitializeComponent();
        Loaded  = MainPage_Loaded;
    }

    private object? SelectedItemWhenPopupOpened { get; set; }

   private void Page_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
    {
        if (CobProduct.FindChildrenOfType<Popup>()?.FirstOrDefault() is Popup popup)
        {
            popup.Opened  = Popup_Opened; 
        }
        if (CobProduct.FindChildrenOfType<TextBox>()?.FirstOrDefault() is TextBox textBox)
        {
            textBox.TextChanged  = TextBox_TextChanged;
        }
    }

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (string.IsNullOrWhiteSpace((sender as TextBox)?.Text))
        {
            CobProduct.SelectedIndex = -1;
            CobProduct.Text = "";
            CobProduct.SelectedValue = null;
            CobProduct.SelectedItem = null;
        }
    }

    private void Popup_Opened(object? sender, object e)
    {
        SelectedItemWhenPopupOpened = this.ComboBoxControl.SelectedItem;
    }

    private void ComboBoxItem_Tapped(object sender, TappedRoutedEventArgs e)
    {
        if (sender.Equals(SelectedItemWhenPopupOpened) is true)
        {
            this.ComboBoxControl.SelectedIndex = -1;
            this.ComboBoxControl.Text = "";
        }
    }
}
  • Related