代码之家  ›  专栏  ›  技术社区  ›  Aymeric

如何在右键单击事件上突出显示列表框的项?

  •  0
  • Aymeric  · 技术社区  · 15 年前

    我不知道我是否是第一个问这个问题的人(我搜索了整个黑板),但我没有找到任何答案。正如标题中所说,每当我右键单击列表框时,我都试图突出显示/选择一个项目。

    以下是XAML代码:

    <ListBox Grid.Row="1" x:Name="ContactList" Margin="6" ItemsSource="{Binding ''}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding Status_Image}" Margin="0,0,3,0" />
                    <StackPanel Orientation="Vertical">
                        <TextBlock Height="20" HorizontalAlignment="Left" Text="{Binding Name}" FontWeight="Bold" FontSize="13" Foreground="Black" />
                        <TextBlock Height="20" HorizontalAlignment="Left" Text="{Binding Message}" FontSize="11" Foreground="Gray" />
                    </StackPanel>
        <Image Source="{Binding NotifImg}" Margin="8,0,0,0"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    我知道如何处理右键单击并在按钮或单个元素上显示上下文菜单,但不在绑定的列表框上。如果你对我应该如何进行有任何建议,请随时告诉我,因为我现在被卡住了。

    谢谢你,埃菲斯曼。

    2 回复  |  直到 15 年前
        1
  •  1
  •   Aymeric    15 年前

    <ListBox Grid.Row="1" x:Name="ContactList"ItemsSource="{Binding ''}" MouseRightButtonDown="ContactList_MouseRightButtonDown" MouseRightButtonUp="ContactList_MouseRightButtonUp">
         <ListBox.ItemTemplate>
             <DataTemplate>
                 <StackPanel Orientation="Horizontal" >
                     <TextBlock Height="20" HorizontalAlignment="Left" Text="{Binding Name}" FontWeight="Bold" FontSize="13" Foreground="Black" />
                     <TextBlock Height="20" HorizontalAlignment="Left" Text="{Binding Message}" FontSize="11" Foreground="Gray" />
                     <Image Source="{Binding NotifImg}" Margin="8,0,0,0"/>
                 </StackPanel>
             </DataTemplate>
         </ListBox.ItemTemplate>
    </ListBox>
    

        private void ContactList_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            e.Handled = true;
        }
    
        private void ContactList_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), (sender as ListBox)).OfType<ListBoxItem>().First().IsSelected = true;
        }    
    

        2
  •  0
  •   user1735022    12 年前

    public class CustomListBoxItem : ListBoxItem
    {
        protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
        {
            OnMouseLeftButtonDown(e);
        }
    }
    

    ItemsSource ListBoxItem CustomListBoxItem

    public class ItemsToCustomListBoxItemsConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;
    
            return
                from object item in (IEnumerable) value
                select new CustomListBoxItem
                    {
                        Content = item
                    };
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new InvalidOperationException();
        }
    }
    

    <ListBox
        ...
        ItemsSource="{Binding Converter={StaticResource ItemsToCustomListBoxItemsConverter}}"
        ...>