我有一个用作导航菜单的ListBox。选择项目时,该项目的状态将突出显示。我现在已经实现了一个消息框,当导航离开页面时,如果有未保存的更改。问题是,ListBoxItem的可视状态在单击时更改为选中状态。我需要能够将状态设置为从代码中选择,而不是单击。
有没有一种方法可以重写单击事件,使其不会导致ListBoxItem进入选中状态?然后我就可以做VisualStateManager.GoToState(item, "Selected", true)了。
如果没有,有没有办法为ListBoxItem创建自定义的可视状态?
发布于 2011-08-22 19:21:29
您应该中断项目容器中MouseLeftButtonDown事件的路由,并从视图模型中设置所选项目。例如:
XAML
<ListBox x:Name="lb">
<ListBoxItem>
<TextBlock MouseLeftButtonDown="TextBlock_OnMouseLeftButtonDown" Text="Test"/>
</ListBoxItem>
</ListBox>事件处理程序
private void TextBlock_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//interrups item selection
e.Handled = true;
//here you can show "Do you want navigate from?" dialog
// and if user accepts then show selected item in menu using SelectedItem or SelectedIndex
lb.SelectedIndex = 0;
}https://stackoverflow.com/questions/7123798
复制相似问题