Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 右键单击列表框中的文件时如何标记文件_C#_Wpf - Fatal编程技术网

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

C# 右键单击列表框中的文件时如何标记文件,c#,wpf,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" AllowsTra

我的列表框中有文件,当我右键单击列表框中的文件时,我希望该文件将被标记,我该如何做

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


可以通过实现自定义附加行为来扩展
ListBoxItem
行为:添加右按钮选择特性。有关附加行为的详细信息:

用法(XAML):附加的行为应应用于
ListBox
ListBoxItem
s

请用定义了
ListBoxItemBehavior
类的命名空间的全名替换BEHAVIOR\u NAMESPACE

<Window ...
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Behaviors="clr-namespace:BEHAVIOR_NAMESPACE">
    <ListBox ...
             SelectionMode="Multiple">
      <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
          <Setter Property="Behaviors:ListBoxItemBehavior.IsEnabled" Value="True" />
        </Style>
      </ListBox.ItemContainerStyle>
    </ListBox>

</Window>


希望这能有所帮助。

您能更好地解释一下如何标记文件吗?当您单击列表框中的项目时,它会变成蓝色,这就是我的意思。标记编辑无法识别该行我应该写什么?(我的列表框属性在第一条注释中)'Behaviors:ListBoxItemBehavior'未找到。定义了
ListBoxItemBehavior
类的命名空间的全名是什么?我不明白您在添加这两行之后问了什么。我收到了许多错误,例如当前上下文中不存在名称'all my controls',请查看没有这两行的我的更新(列表框和窗口)
<Window ...
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Behaviors="clr-namespace:BEHAVIOR_NAMESPACE">
    <ListBox ...
             SelectionMode="Multiple">
      <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
          <Setter Property="Behaviors:ListBoxItemBehavior.IsEnabled" Value="True" />
        </Style>
      </ListBox.ItemContainerStyle>
    </ListBox>

</Window>