C# WPF列表框中,所选索引不是高亮显示的行
奇怪的情况,我有以下XAML:C# WPF列表框中,所选索引不是高亮显示的行,c#,wpf,listbox,C#,Wpf,Listbox,奇怪的情况,我有以下XAML: <StackPanel> <TextBlock>Logging here...</TextBlock> <ListBox x:Name="LoggingListBox" ItemsSource="{Binding Log}" /> </StackPanel> 但它显示如下: 如您所见,第三项确实有边框,但为什么第一行高亮显示 有人知道发生了什么事吗 2017年1月17日更新: 我已经做了一个
<StackPanel>
<TextBlock>Logging here...</TextBlock>
<ListBox x:Name="LoggingListBox" ItemsSource="{Binding Log}" />
</StackPanel>
但它显示如下:
如您所见,第三项确实有边框,但为什么第一行高亮显示
有人知道发生了什么事吗
2017年1月17日更新: 我已经做了一个小的演示版本的问题 1) 新WPF项目 2) MainWindow.xaml:
<Window x:Class="ListBoxSelectionTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<StackPanel>
<TextBlock>Logging here...</TextBlock>
<ListBox x:Name="LoggingListBox" ItemsSource="{Binding Log}" />
</StackPanel>
</Window>
在这里登录。。。
3) 代码隐藏:
public partial class MainWindow
{
public ObservableCollection<string> Log { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
Log = new ObservableCollection<string>();
Log.CollectionChanged += (o, e) => LoggingListBox.SelectedItem = Log.Last();
Log.Add("Adding instrument: Unknown:#22");
Log.Add("Adding instrument: Unknown:#22");
Log.Add("Adding instrument: Unknown:#22");
Log.Add("Adding instrument: Unknown:#22");
Log.Add("Adding instrument: Unknown:#22");
}
}
公共部分类主窗口
{
公共ObservableCollection日志{get;set;}
公共主窗口()
{
初始化组件();
DataContext=this;
Log=新的ObservableCollection();
Log.CollectionChanged+=(o,e)=>LoggingListBox.SelectedItem=Log.Last();
添加(“添加工具:未知:#22”);
添加(“添加工具:未知:#22”);
添加(“添加工具:未知:#22”);
添加(“添加工具:未知:#22”);
添加(“添加工具:未知:#22”);
}
}
4:运行程序:
启动时看起来像这样:
5:点击TAB按钮(将焦点设置为listbox),现在看起来像:
我怀疑我在某种程度上设置了WPF真正想要的选择顺序。看起来像是它选择的项目,但没有突出显示。聚焦时,您可以尝试高亮显示为青色
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Cyan"/>
不聚焦时呈灰色
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Gray" />
看起来像您选择的项目,但没有突出显示。聚焦时,您可以尝试高亮显示为青色
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Cyan"/>
不聚焦时呈灰色
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Gray" />
列表框设计用于处理多个选择,突出显示是一种单独的情况。您还需要将其滚动到视图中,以将突出显示更改为所选的此类图像
LoggingListBox.ScrollIntoView(LoggingListBox.Items[3]);
列表框
用于处理多个选择,突出显示是一种单独的情况。您还需要将其滚动到视图中,以将突出显示更改为所选的此类图像
LoggingListBox.ScrollIntoView(LoggingListBox.Items[3]);
在你的编辑之后,你会更清楚你的情况 首先,请注意,对示例进行此小的测试更改可以解决此问题:
Log.Add("Adding instrument: Unknown:#22");
Log.Add("Adding instrument: Unknown:#23");
Log.Add("Adding instrument: Unknown:#24");
Log.Add("Adding instrument: Unknown:#25");
Log.Add("Adding instrument: Unknown:#26");
现在每根弦都不同了。所以第一行是灰色\蓝色的原因是ListBox基本上无法区分它们
但我们为什么要在边境上散布星点呢?嗯,那是因为它是由另一种机制画出来的-。而且它似乎能够正确地跟踪选定的视觉元素
对于这种情况,可能有不同的解决方案,但是为什么不将选择逻辑移到索引中,然后使用它呢
LoggingListBox.Loaded += (o, e) =>
{
Log.CollectionChanged += (oo, ee) => SelectLastEntry();
SelectLastEntry();
};
...
private void SelectLastEntry()
{
LoggingListBox.SelectedIndex = LoggingListBox.Items.Count - 1;
}
在你的编辑之后,你会更清楚你的情况 首先,请注意,对示例进行此小的测试更改可以解决此问题:
Log.Add("Adding instrument: Unknown:#22");
Log.Add("Adding instrument: Unknown:#23");
Log.Add("Adding instrument: Unknown:#24");
Log.Add("Adding instrument: Unknown:#25");
Log.Add("Adding instrument: Unknown:#26");
现在每根弦都不同了。所以第一行是灰色\蓝色的原因是ListBox基本上无法区分它们
但我们为什么要在边境上散布星点呢?嗯,那是因为它是由另一种机制画出来的-。而且它似乎能够正确地跟踪选定的视觉元素
对于这种情况,可能有不同的解决方案,但是为什么不将选择逻辑移到索引中,然后使用它呢
LoggingListBox.Loaded += (o, e) =>
{
Log.CollectionChanged += (oo, ee) => SelectLastEntry();
SelectLastEntry();
};
...
private void SelectLastEntry()
{
LoggingListBox.SelectedIndex = LoggingListBox.Items.Count - 1;
}
这很奇怪,如果这是唯一的代码,一切都应该正常。您使用什么
SelectionMode
?除了设置SelectedIndex外,您还对selection执行其他操作吗?在哪里调用所选索引的更改(即按钮单击处理程序)?这很奇怪,如果这是唯一的代码,那么一切都应该工作。您使用什么SelectionMode
?除了设置SelectedIndex外,您还对selection执行其他操作吗?在何处调用所选索引的更改(即按钮单击处理程序)?