2015-07-07 3 views
0

Мой ListBox использует этот DataTemplate действовать на LeftDoubleClick:Listbox элемент мыши двойной щелчок работает только на текст не всей линии

<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

ответ

0

Set ListBoxItems 'HorizontalContentAlignment = Stretch.

Например:

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

В случае, приведенный выше код не работает, попробуйте также установив Background из Grid в Transparent (если вы используете Grid):

<Grid Background="Transparent"> 

EDIT:

Bind TextBlock Width to ListBoxItem Width, как показано ниже, необходимо исправить проблему:

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

Пробовал оба, Стиль Grid Background. Проблема все еще существует. – Rainer

+0

Обновлен код, теперь отображается полный код XAML – Rainer

+0

@Rainer - привяжите ширину TextBlock к ширине ListBoxItem, как показано в моем Отредактированном ответе. – Vinkal