[英]Listbox item mouse double click only works on text not whole line
我的列表框使用此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>
雙擊僅適用於項目行中的實際文本。 我該怎么做才能讓雙擊也可以在項目文本右側的空白處起作用?
問候雷納
設置ListBoxItems
的HorizontalContentAlignment = Stretch
。
例如:
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
如果上面的代碼不起作用,還可以嘗試將Grid
的Background
設置為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}">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.