有什么办法可以实现像办公室撤消下拉(图像波纹管)? 我的意思是,当用户将鼠标放在第一个以外的项目上时,我想突出显示上一个项目? 我试图从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
。这就是我们所需要的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
辅助方法,其被用于确定如果光标在项目上,被定义如下:
private bool IsMouseOverItem(Visual item, Point mouseOverPoint)
{
Rect currentDescendantBounds = VisualTreeHelper.GetDescendantBounds(item);
return currentDescendantBounds.Contains(mouseOverPoint);
}
最后,作为ListBox的点击处理程序的listBox_SelectedChanged
事件处理程序:
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高亮颜色。随意更改并试一试。
猜你需要这样的事:
<ControlTemplate TargetType="ListBoxItem">
<TextBlock Text="{Binding LastOperation}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger>
<DataTrigger.Binding>
<MultiBinding>
<Binding Path="MouseOverIndex"/>
<Binding Path="CurrentIndex"/>
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Foreground" Value="Gold"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</ControlTemplate>
感谢您的回复,但您如何绑定MouseOverIndex? –
MouseOverIndex和CurrentIndex是用于此UI的视图模型的所有属性。您可以将MouseOverIndex绑定到另一个DataTrigger,其中的代码位于逻辑后面,如if(MouseOverIndexNewValue> MouseOverIndexOldValue){MouseOverIndexOldValue = MouseOverIndexNewValue; } – Nathan
- 1. QTreeView:获取仅高亮显示的项目列表(多选)
- 2. 火花表中的高亮项目
- 3. ListView中的高亮项目
- 4. silverlight 3列表框项目高亮与所选
- 5. XAML列表框项目没有亮点
- 6. 如何保存列表视图项目作为高亮项目
- 7. 列表框项高亮显示或在我的列表框选择的混乱
- 8. 从列表框拖放多个项目
- 9. 我有一个列表框。如何从多个项目中选择列表框中的第三个项目?
- 10. C#/ WinForms:通过拖放仅高亮列表框的第一项
- 11. 高亮所选项目
- 12. Python - 如何提高列表中多个项目的值错误
- 13. 第一项总是被读,而不是选择(高亮)项的列表框中
- 14. 如何获取extjs组合框中高亮显示的项目?
- 15. 高亮显示多个选择选项
- 16. 列表框项目分隔符多个项目时
- 17. 添加项目到多列列表框
- 18. 从列表框中删除多个项目; XML中的更新
- 19. 从GridView列中选择多个列表框项目
- 20. 将列表框1中的项目添加到绑定多项列表框2
- 21. 将多个列表框项添加到一个列表框中
- 22. tkinter删除列表框中的多个未选定的项目
- 23. 多个列表中的项目
- 24. 查找多个列表中的项目?
- 25. 下拉列表中的多个项目
- 26. Python:re.sub列表中有多个项目的单个项目
- 27. 列表框中的多个默认选定项目
- 28. vb:如何一次选择列表框中的多个项目?
- 29. 无法为列表框中的多个项目执行宏
- 30. 如何从多个列表框中删除相同的项目?
感谢您的详细解释。 –