列表框项鼠标双击只对文本有效,而不是整行

本文关键字:有效 文本 鼠标 双击 列表 | 更新日期: 2023-09-27 18:01:19

我的ListBox使用这个DataTemplate来作用于LeftDoubleClick:

<UserControl x:Class="X1.XPrep.GuiModuleJobSelection.Views.ContentJobSelectionView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:l="http://wpflocalizeextension.codeplex.com"
         xmlns:Classes="clr-namespace:X1.XPrep.GuiModuleJobSelection.Models"
         xmlns:ViewModels="clr-namespace:X1.XPrep.GuiModuleJobSelection.ViewModels"
         l:LocalizeDictionary.DesignCulture="en"
         l:ResxLocalizationProvider.DefaultAssembly="XPrep.GuiModuleJobSelection"
         l:ResxLocalizationProvider.DefaultDictionary="Resources"
         x:Name="jobSelectionContent"
         mc:Ignorable="d" 
         d:DataContext="{d:DesignInstance {x:Type ViewModels:ContentJobSelectionViewModel}, IsDesignTimeCreatable=True}"
         d:DesignHeight="600" d:DesignWidth="328.5">
<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/XPrep.GuiCommon;component/Resources/Styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <DataTemplate x:Key="ListboxJobDataTemplate" DataType="{x:Type Classes:JobForJobSelectionViewModel}">
            <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding JobData.JobName}" 
                       VerticalAlignment="Top">
                <TextBlock.ToolTip>
                    <StackPanel Height="200" MinWidth="200">
                        <StackPanel Width="200" Orientation="Vertical" >
                            <TextBlock Text="{Binding JobData.JobName}" FontWeight="Bold"/>
                            <Image VerticalAlignment="Top" Source="{Binding PreviewImageSource}" Margin="0,10,0,0"/>
                            <TextBlock Text="No preview image available." Margin="0,10,0,0">
                                <TextBlock.Style>
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Path=IsPreviewImageMissing}" Value="False">
                                                <Setter Property="Visibility" Value="Hidden"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                            <TextBlock Text="{Binding JobData.Comment}"/>
                        </StackPanel>
                    </StackPanel>
                </TextBlock.ToolTip>
                <TextBlock.InputBindings>
                    <MouseBinding Gesture="LeftDoubleClick"
                        Command="{Binding Path=DataContext.LoadCommand, 
                        RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}}}"/>
                </TextBlock.InputBindings>
            </TextBlock>
        </DataTemplate>
    </ResourceDictionary>
</UserControl.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <ListBox ItemsSource="{Binding Jobs, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" 
             SelectedItem="{Binding SelectedJob, Mode=TwoWay}" ItemTemplate="{DynamicResource ListboxJobDataTemplate}"
             Grid.Column="1" Grid.Row="0" x:Name="listBoxJobNames" MinWidth="200"                 
             HorizontalAlignment="Stretch"  Margin="6" VerticalAlignment="Stretch">
        <ListBox.InputBindings>
            <KeyBinding Key="Delete" Command="{Binding Path=DataContext.DeleteCommand, 
                        RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}}}"/>
        </ListBox.InputBindings>
    </ListBox>
    <StackPanel Grid.Column="0" Grid.Row="0" Margin="0" Width="100" Background="{DynamicResource ContextBackgroundBrush}">
        <Button x:Name="jobSelectionNewButton" Content="{DynamicResource X1.Job.New}" ToolTip="{l:Loc JobSelection_Button_New}"
                HorizontalAlignment="Left" Margin="6" VerticalAlignment="Top" Command="{Binding NewCommand}" IsDefault="True"
                Style="{StaticResource GlossyButtonX1}"/>
        <Button Content="{DynamicResource X1.Job.Load}" ToolTip="{l:Loc JobSelection_Button_Load}" 
                HorizontalAlignment="Left" Margin="6" VerticalAlignment="Top"
                Style="{StaticResource GlossyButtonX1}" 
                Command="{Binding LoadCommand}"/>
        <Button Content="{DynamicResource X1.Job.Rename}" ToolTip="{l:Loc JobSelection_Button_Rename}" 
                HorizontalAlignment="Left" Margin="6" VerticalAlignment="Top" 
                Style="{StaticResource GlossyButtonX1}" 
                Command="{Binding RenameCommand}"/>
        <Button Content="{DynamicResource X1.Job.Clone}" ToolTip="{l:Loc JobSelection_Button_Clone}" 
                HorizontalAlignment="Left" Margin="6" VerticalAlignment="Top" 
                Style="{StaticResource GlossyButtonX1}" 
                Command="{Binding CloneCommand}"/>
        <Button Content="{DynamicResource X1.Common.Delete}" ToolTip="{l:Loc JobSelection_Button_Delete}" 
                HorizontalAlignment="Left" Margin="6" VerticalAlignment="Top"
                Style="{StaticResource GlossyButtonX1}" 
                Command="{Binding DeleteCommand}"/>
    </StackPanel>
</Grid>

双击只作用于项目行的实际文本。我能做些什么,让双击也工作在空白的空间,我的项目文本的权利?

的问候Rainer

列表框项鼠标双击只对文本有效,而不是整行

Set ListBoxItems ' HorizontalContentAlignment = Stretch .

例如:

<Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>

如果上面的代码不起作用,也可以尝试将GridBackground设置为Transparent(如果您使用的是Grid):

<Grid Background="Transparent">
编辑:

TextBlock Width绑定到ListBoxItem Width,如下所示,应该可以解决这个问题:

<TextBlock  HorizontalAlignment="Left" 
            TextWrapping="Wrap" 
            Text="{Binding JobData.JobName}" 
            VerticalAlignment="Top"
            Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=ActualWidth}">