0

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

这是 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>

我知道如何处理右键单击并在按钮或单个元素上显示上下文菜单,而不是在绑定的列表框上。如果您对我应该如何进行有任何建议,请随时告诉我,因为我目前陷入困境。

谢谢你,以弗所。

4

2 回答 2

1

好吧,我找到了一种非常简单而干净的方法来实现我想做的事情!

这是 XAML 代码:

<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;
    }    

不要忘记为“OfType”包含 System.Linq。

以弗斯门。

于 2010-10-06T07:59:17.237 回答
0

多选模式解决方案

上面 Ephismen 的解决方案对于多选模式下的 ListBox 无法正常工作(例如,当 Ctrl 按下时它不会切换项目的选定状态,当 Ctrl 没有按下时它不会取消选择其他项目,...)。

我建议使用自定义的鼠标右键单击处理程序来创建自定义 ListBoxItem。在那里您可以模拟鼠标左键单击,从而获得完全相同的行为:

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();
    }
}

下面是 ItemsSource 绑定的样子:

<ListBox
    ...
    ItemsSource="{Binding Converter={StaticResource ItemsToCustomListBoxItemsConverter}}"
    ...>
于 2013-10-01T11:31:28.847 回答