列表框项目初始化但是一旦选择了一个我不知道如何重置没有选择任何项目的列表框
The Listbox items initialize but once one is selected I don't know how to RESET the listbox where neither item is selected
如果您 运行 代码,则不会 select 编辑任何列表框项目。如果您 select 任其一,它仍然 selected 没问题,并相应地在文本框中显示“一”或“二”。单击 ResetListBox 按钮时,selected 项目被取消selected(?也许)但保留灰色背景(不需要)。一旦该项目具有此浅灰色背景,就不再触发 onClick 事件...不会向文本框添加其他文本。这个问题已在网络上以各种形式被问到,我在这个简单示例中尝试的答案中 none 有效。
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="279.186" Width="401.98">
<Grid Margin="0,0,-6.8,-2.4">
<ListBox x:Name="ThanklessListBox" HorizontalAlignment="Left" Height="89" Margin="24,25,0,0" VerticalAlignment="Top" Width="117">
<ListBoxItem x:Name="ItemI" Content="ItemUno" Selected="ItemI_Selected"/>
<ListBoxItem x:Name="Item2" Content="ItemDos" Selected="Item2_Selected"/>
</ListBox>
<TextBox x:Name="StuffToShow" HorizontalAlignment="Left" Height="178" Margin="198,25,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="116"/>
<Button x:Name="ResetListBox" Content="ResetListBox" HorizontalAlignment="Left" Height="26" Margin="28,131,0,0" VerticalAlignment="Top" Width="116" Click="ResetListBox_Click"/>
<Button x:Name="SeleectButton" Content="SelectItemDos" HorizontalAlignment="Left" Height="24" Margin="28,179,0,0" VerticalAlignment="Top" Width="116" Click="SeleectButton_Click"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ItemI_Selected(object sender, RoutedEventArgs e)
{
StuffToShow.Text += "\nOne";
}
private void Item2_Selected(object sender, RoutedEventArgs e)
{
StuffToShow.Text += "\nTwo";
}
private void SeleectButton_Click(object sender, RoutedEventArgs e)
{
ThanklessListBox.SelectedItem = 1; //Choose Dos
}
private void ResetListBox_Click(object sender, RoutedEventArgs e)
{
ThanklessListBox.SelectedItem = -1; //Deselect
}
}
在您后面的代码中,您使用的是带整数的 SelectedItem。
private void ResetListBox_Click(object sender, RoutedEventArgs e)
{
ThanklessListBox.SelectedItem = -1; //Deselect
}
尝试使用 SelectedIndex
private void ResetListBox_Click(object sender, RoutedEventArgs e)
{
ThanklessListBox.SelectedIndex = -1; //Deselect
}
或所选项目为空
编辑添加:您也可以这样做
ThanklessListBox.SelectedItems.Clear
这就是我认为 post 现在被删除的另一个人的意思>
平心而论,这是您真正可以通过浏览 MS 站点上的 online documentation for ListBox 来学习的东西
如果您 运行 代码,则不会 select 编辑任何列表框项目。如果您 select 任其一,它仍然 selected 没问题,并相应地在文本框中显示“一”或“二”。单击 ResetListBox 按钮时,selected 项目被取消selected(?也许)但保留灰色背景(不需要)。一旦该项目具有此浅灰色背景,就不再触发 onClick 事件...不会向文本框添加其他文本。这个问题已在网络上以各种形式被问到,我在这个简单示例中尝试的答案中 none 有效。
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="279.186" Width="401.98">
<Grid Margin="0,0,-6.8,-2.4">
<ListBox x:Name="ThanklessListBox" HorizontalAlignment="Left" Height="89" Margin="24,25,0,0" VerticalAlignment="Top" Width="117">
<ListBoxItem x:Name="ItemI" Content="ItemUno" Selected="ItemI_Selected"/>
<ListBoxItem x:Name="Item2" Content="ItemDos" Selected="Item2_Selected"/>
</ListBox>
<TextBox x:Name="StuffToShow" HorizontalAlignment="Left" Height="178" Margin="198,25,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="116"/>
<Button x:Name="ResetListBox" Content="ResetListBox" HorizontalAlignment="Left" Height="26" Margin="28,131,0,0" VerticalAlignment="Top" Width="116" Click="ResetListBox_Click"/>
<Button x:Name="SeleectButton" Content="SelectItemDos" HorizontalAlignment="Left" Height="24" Margin="28,179,0,0" VerticalAlignment="Top" Width="116" Click="SeleectButton_Click"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ItemI_Selected(object sender, RoutedEventArgs e)
{
StuffToShow.Text += "\nOne";
}
private void Item2_Selected(object sender, RoutedEventArgs e)
{
StuffToShow.Text += "\nTwo";
}
private void SeleectButton_Click(object sender, RoutedEventArgs e)
{
ThanklessListBox.SelectedItem = 1; //Choose Dos
}
private void ResetListBox_Click(object sender, RoutedEventArgs e)
{
ThanklessListBox.SelectedItem = -1; //Deselect
}
}
在您后面的代码中,您使用的是带整数的 SelectedItem。
private void ResetListBox_Click(object sender, RoutedEventArgs e)
{
ThanklessListBox.SelectedItem = -1; //Deselect
}
尝试使用 SelectedIndex
private void ResetListBox_Click(object sender, RoutedEventArgs e)
{
ThanklessListBox.SelectedIndex = -1; //Deselect
}
或所选项目为空
编辑添加:您也可以这样做
ThanklessListBox.SelectedItems.Clear
这就是我认为 post 现在被删除的另一个人的意思>
平心而论,这是您真正可以通过浏览 MS 站点上的 online documentation for ListBox 来学习的东西