当我清除ComboBox文本或再次单击时,所选项目仍未取消。
<ComboBox
MinWidth="120"
IsEditable="True"
PlaceholderText="please select">
<ComboBoxItem>A</ComboBoxItem>
<ComboBoxItem>B</ComboBoxItem>
<ComboBoxItem>C</ComboBoxItem>
</ComboBox>
发布于 2022-11-05 13:15:59
试着这样做:
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;
}
}
}
}
}
发布于 2022-11-05 14:42:06
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 (this.ComboBoxControl.FindChildrenOfType<Popup>()?.FirstOrDefault() is Popup popup)
{
popup.Opened += Popup_Opened;
}
if (this.ComboBoxControl.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))
{
this.ComboBoxControl.SelectedIndex = -1;
this.ComboBoxControl.Text = "";
this.ComboBoxControl.SelectedValue = null;
this.ComboBoxControl.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 = "";
}
}
}
https://stackoverflow.com/questions/74319843
复制相似问题