gpt4 book ai didi

c# - 突出显示列表框中的多个项目

转载 作者:行者123 更新时间:2023-12-03 10:59:56 24 4
gpt4 key购买 nike

有什么办法可以实现像Office undo drop down(图像波纹管)这样的功能?
我的意思是,我想在用户将鼠标悬停在除第一个项目以外的其他项目上时突出显示上一个项目吗?
我尝试了FluentRibbon的一些控件,但到目前为止还没有运气。

最佳答案

在大多数情况下,这样的控件设计是在Blend中完成的。但是,如果您不知道如何使用Blend,则仅使用XAML和后台代码仍然可以实现类似的结果,但是您需要做更多的工作。

我们首先创建一个名为CustomListBoxItem的类,该类继承自ListBoxItem。然后,我们定义一个依赖项属性,该属性用于突出显示列表框中的项目:

public class CustomListBoxItem : ListBoxItem
{
public static readonly DependencyProperty IsVirtuallySelectedProperty =
DependencyProperty.Register("IsVirtuallySelected", typeof(bool),
typeof(CustomListBoxItem),
new PropertyMetadata(false));

public CustomListBoxItem() : base()
{ }

public bool IsVirtuallySelected
{
get { return (bool)GetValue(IsVirtuallySelectedProperty); }
set { SetValue(IsVirtuallySelectedProperty, value); }
}
}

然后,我们添加一个列表框并在XAML中为其定义样式:
<ListBox Name="listBox" MouseMove="listBox_MouseMove" SelectionChanged="listBox_SelectionChanged">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type local:CustomListBoxItem}">
<Style.Triggers>
<Trigger Property="IsVirtuallySelected" Value="true">
<Setter Property="Background" Value="SkyBlue"/>
</Trigger>
<Trigger Property="IsVirtuallySelected" Value="false">
<Setter Property="Background" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>

其中 local是定义 CustomListBoxItem的 namespace 。这就是XAML部分所需要的,真正的魔力发生在后面的代码中。
listBox_MouseMove事件处理程序如下所示:
private void listBox_MouseMove(object sender, MouseEventArgs e)
{
bool itemFound = false;

for (int i = 0; i < listBox.Items.Count; i++)
{
var currentItem = listBox.ItemContainerGenerator.ContainerFromIndex(i) as CustomListBoxItem;

if (currentItem == null)
continue;

// Check whether the cursor is on an item or not.
if (IsMouseOverItem(currentItem, e.GetPosition((IInputElement)currentItem)))
{
// Unselect all items before selecting the new group
listBox.Items.Cast<CustomListBoxItem>().ToList().ForEach(x => x.IsVirtuallySelected = false);

// Select the current item and the ones above it
for (int j = 0; j <= listBox.Items.IndexOf(currentItem); j++)
{
((CustomListBoxItem)listBox.Items[j]).IsVirtuallySelected = true;
}

itemFound = true;
break;
}
}

// If the item wasn't found for the mouse point, it means the pointer is not over any item, so unselect all.
if (!itemFound)
{
listBox.Items.Cast<CustomListBoxItem>().ToList().ForEach(x => x.IsVirtuallySelected = false);
}
}

用来确定光标是否在项目上的 IsMouseOverItem helper方法定义如下:
 private bool IsMouseOverItem(Visual item, Point mouseOverPoint)
{
Rect currentDescendantBounds = VisualTreeHelper.GetDescendantBounds(item);
return currentDescendantBounds.Contains(mouseOverPoint);
}

最后是 listBox_SelectedChanged事件处理程序,它充当ListBox的单击处理程序:
 private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Get all the virtually selected items
List<CustomListBoxItem> selectedItems =
listBox.Items.Cast<CustomListBoxItem>().Where(x => x.IsVirtuallySelected).ToList();

if (selectedItems == null || !selectedItems.Any())
return;

// Do something with the selected items
DoCoolStuffWithSelectedItems();

// Unselsect all.
listBox.Items.Cast<CustomListBoxItem>().ToList().ForEach(x => x.IsVirtuallySelected = false);
listBox.UnselectAll();
}

繁荣,我们完成了。现在,我们可以在类构造函数的ListBox中添加一些项目:
 public MainWindow()
{
InitializeComponent();


listBox.Items.Add(new CustomListBoxItem { Content = "hello world!" });
listBox.Items.Add(new CustomListBoxItem { Content = "wpf is cool" });
listBox.Items.Add(new CustomListBoxItem { Content = "today is tuesday..." });
listBox.Items.Add(new CustomListBoxItem { Content = "I like coffee" });
}

请注意,我在XAML中使用随机颜色作为突出显示颜色。随时进行更改并尝试一下。

关于c# - 突出显示列表框中的多个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17430535/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com