右键单击列表框中的文件时如何标记文件

时间:2012-03-04 15:40:58

标签: c# wpf

我有包含文件的列表框,当我右键单击列表框中的文件时,我希望文件将被标记,我该怎么办?

<Window x:Class="myTool.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="myTool" AllowsTransparency="False" Icon="/myTool;component/Images/Organize.ico" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="594" Width="512" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize" >

        <ListBox Height="95" HorizontalAlignment="Left" Margin="78,35,0,0" Name="listBoxFiles" VerticalAlignment="Top" Width="323" Grid.ColumnSpan="2" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" SelectionMode="Multiple" PreviewMouseRightButtonDown="listBoxFiles_PreviewMouseRightButtonDown" >
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">

                    <EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_DoubleClick" />
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Delete" PreviewMouseDown="MenuItem_PreviewMouseDown" Click="MenuItemDelete_Click"></MenuItem>
                </ContextMenu>
            </ListBox.ContextMenu>
        </ListBox>

1 个答案:

答案 0 :(得分:1)

可以通过实现自定义附加行为来扩展ListBoxItem行为:添加正确的按钮选择方面。有关附加行为的更多信息:Introduction to Attached Behaviors in WPF

public static class ListBoxItemBehavior
{
    #region IsEnabled

    public static bool GetIsEnabled(ListBoxItem listBoxItem)
    {
        return (bool)listBoxItem.GetValue(IsEnabledProperty);
    }

    public static void SetIsEnabled(ListBoxItem listBoxItem, bool value)
    {
        listBoxItem.SetValue(IsEnabledProperty, value);
    }

    public static readonly DependencyProperty IsEnabledProperty =
        DependencyProperty.RegisterAttached(
        "IsEnabled",
        typeof(bool),
        typeof(ListBoxItemBehavior),
        new UIPropertyMetadata(false, OnIsEnabledChanged));

    static void OnIsEnabledChanged(
      DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        ListBoxItem item = depObj as ListBoxItem;
        if (item == null)
            return;

        if (e.NewValue is bool == false)
            return;

        if ((bool)e.NewValue)
            item.MouseRightButtonDown += ItemOnMouseRightButtonDown;
        else
            item.MouseRightButtonDown -= ItemOnMouseRightButtonDown;
    }

    private static void ItemOnMouseRightButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
    {
        ListBoxItem item = mouseButtonEventArgs.OriginalSource as ListBoxItem;
        if (item != null)
        {
            item.IsSelected = !item.IsSelected;
        }
    }

    #endregion
}

用法(XAML):附加行为应适用于ListBoxItem的{​​{1}}。

请将 BEHAVIOR_NAMESPACE 替换为定义ListBox类的命名空间的全名。

ListBoxItemBehavior

希望这有帮助。