2010-08-27 152 views
0

我有一個小問題。WPF ComboBox駐留在列表框中單擊事件

我有一個ListBox綁定到一個對象列表。這些對象有一個可以設置的屬性。我在LIstBox的ItemTemplate(一個datatemplate)中有一個關於對象屬性的組合框,組合框有一些硬編碼的值。

我的問題是,當列表框被顯示,我點擊ont他組合,只有ListBoxItem被選中,點擊從不使它到組合框!

只是給你一個想法。

<ListBox SelectionMode="Multiple" VerticalAlignment="Center" HorizontalAlignment="Left" Style="{StaticResource Style_ListBox}" ItemsSource="{Binding ModeSampleSets, Mode=OneWay}"> 
       <ListBox.ItemTemplate> 

        <DataTemplate DataType="ListBoxItem"> 
         <Grid> 
          <StackPanel> 

           <Grid> 
            <Grid.ColumnDefinitions> 
             <ColumnDefinition Width="Auto"/> 
             <ColumnDefinition Width="*"/> 
             <ColumnDefinition Width="Auto"/> 
            </Grid.ColumnDefinitions> 

            <TextBlock Grid.Column="0" FontWeight="Bold" Text="{Binding Name, Mode=OneWay}" Width="{Binding ElementName=this, Path=Content.DesiredWidth}"/> 


            <ComboBox Focusable="False" Width="10" Height="10" Grid.Column="2" Style="{StaticResource Style_ComboBoxColorPicker}" SelectedItem="{Binding GraphColor, Mode=TwoWay}" > 
             <ComboBoxItem IsSelected="True" Content="#e62a2c" /> 
             <ComboBoxItem Content="#ec7c28"></ComboBoxItem> 
             <ComboBoxItem Content="#69c5d8"></ComboBoxItem> 
             <ComboBoxItem Content="#36b34b"></ComboBoxItem> 
             <ComboBoxItem Content="#415dae"></ComboBoxItem> 
             <ComboBoxItem Content="#9056A3"></ComboBoxItem> 
             <ComboBoxItem Content="#0b0b0b"></ComboBoxItem> 
             <ComboBoxItem Content="#666666"></ComboBoxItem> 
             <ComboBoxItem Content="#a6a6a6"></ComboBoxItem> 
            </ComboBox> 


           </Grid> 

           <StackPanel Orientation="Horizontal"> 
            <TextBlock FontSize="8" Text="{Binding SampleAnalysisDate, Mode=OneWay}" Width="{Binding ElementName=this, Path=Content.DesiredWidth}"/> 
           </StackPanel> 
           <StackPanel Orientation="Horizontal" Margin="0,4,0,0"> 
            <TextBlock FontSize="14" Text="{Binding WaveLengthStart, Mode=OneWay}" Width="{Binding ElementName=this, Path=Content.DesiredWidth}"/> 
            <TextBlock Margin="2,0,0,0" FontSize="14" Text="{x:Static UIStrings:WaveLengthScanStrings.WaveLengthScanModeView_SampleWaveLength_Seperator}" Width="{Binding ElementName=this, Path=Content.DesiredWidth}"/> 
            <TextBlock Margin="2,0,0,0" FontSize="14" Text="{Binding WaveLengthStop, Mode=OneWay}" Width="{Binding ElementName=this, Path=Content.DesiredWidth}"/> 
           </StackPanel> 
          </StackPanel> 

         </Grid> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
</ListBox> 

樣式:

<Style x:Key="Style_ComboBoxColorPickerItemContainerStyle" TargetType="ComboBoxItem"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ComboBoxItem"> 
       <Border Name="ItemBorder" Margin="2" BorderBrush="Transparent" BorderThickness="1.5" Background="Transparent"> 
        <ContentPresenter Margin="2" Height="20" Width="20" ContentTemplate="{StaticResource DataTemplate_ComboBoxColorPickerItemTemplate}" /> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsSelected" Value="True"> 
         <Setter TargetName="ItemBorder" Property="BorderBrush" Value="Black"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 


<Style x:Key="Style_ComboBoxColorPicker" TargetType="ComboBox"> 
    <Setter Property="ItemsPanel"> 
     <Setter.Value> 
      <ItemsPanelTemplate> 
       <WrapPanel MaxWidth="100"/> 
      </ItemsPanelTemplate> 
     </Setter.Value> 
    </Setter> 

    <Setter Property="ItemTemplate" Value="{StaticResource DataTemplate_ComboBoxColorPickerItemTemplate}"/> 

    <Setter Property="ItemContainerStyle" Value="{StaticResource Style_ComboBoxColorPickerItemContainerStyle}"/> 

    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ComboBox"> 

       <Grid> 

        <ToggleButton 
            ClickMode="Press" 
            Name="ComboToggleButton" 
            IsChecked="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" 
            Focusable="False" 
            > 
         <ToggleButton.Template> 
          <ControlTemplate TargetType="ToggleButton"> 
           <ContentPresenter/> 
          </ControlTemplate> 
         </ToggleButton.Template> 

         <ContentPresenter Content="{TemplateBinding ComboBox.SelectionBoxItem}" ContentTemplate="{TemplateBinding ComboBox.ItemTemplate}" ContentTemplateSelector="{TemplateBinding ComboBox.ItemTemplateSelector}"/> 

        </ToggleButton> 



        <Popup     
            Placement="Bottom"     
            Name="Popup"     
            Focusable="False"     
            AllowsTransparency="True"     
            IsOpen="{TemplateBinding ComboBox.IsDropDownOpen}"    
            PopupAnimation="Fade"> 

         <Grid     
             MinWidth="{TemplateBinding FrameworkElement.ActualWidth}"     
             MaxHeight="{TemplateBinding ComboBox.MaxDropDownHeight}"     
             Name="DropDown"     
             SnapsToDevicePixels="True"> 
          <Border      
              BorderBrush="Gray"      
              BorderThickness="1.5"      
              Name="DropDownBorder"      
              Background="White"> 
           <ScrollViewer      
               Margin="0"      
               SnapsToDevicePixels="True"> 
            <ItemsPresenter /> 
           </ScrollViewer> 
          </Border> 
         </Grid> 
        </Popup> 
       </Grid> 
      </ControlTemplate> 

     </Setter.Value> 
    </Setter> 
</Style> 

<Style x:Key="Style_ListBox" TargetType="ListBox"> 
    <Setter Property="BorderBrush" Value="{StaticResource Brush_PanelInnerBorder}"/> 
    <Setter Property="BorderThickness" Value="0"/> 
    <Setter Property="Background" Value="Transparent"/> 
    <Setter Property="Margin" Value="5"/> 
    <Setter Property="ScrollViewer.CanContentScroll" Value="False"/> 

    <Setter Property="ItemsPanel"> 
     <Setter.Value> 
      <ItemsPanelTemplate> 
       <StackPanel Background="{StaticResource Brush_PanelInnerBackground}"/> 
      </ItemsPanelTemplate> 
     </Setter.Value> 
    </Setter> 

    <Setter Property="ItemContainerStyle"> 
     <Setter.Value> 



      <Style TargetType="{x:Type ListBoxItem}"> 
       <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/> 
       <Setter Property="Template"> 
        <Setter.Value> 


         <ControlTemplate TargetType="{x:Type ListBoxItem}"> 



          <Grid Background="{StaticResource Brush_PanelInnerBackground}" Margin="-.5"> 
           <Border x:Name="BorderItem" Margin="0" ClipToBounds="True" BorderThickness="0" Style="{StaticResource Style_PanelInnerBorder}"> 
            <Rectangle x:Name="BackgroundRec" Fill="Transparent" Stroke="Transparent" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" HorizontalAlignment="Left" VerticalAlignment="Top"/> 
           </Border> 
           <ContentPresenter Name="TheContentPresenter" Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
           <Border Margin="0" ClipToBounds="True" Style="{StaticResource Style_PanelInnerBorder}"> 
            <Rectangle VerticalAlignment="Bottom" Width="{TemplateBinding Width}" Height="0"/> 
           </Border> 
          </Grid> 

          <ControlTemplate.Triggers> 
           <Trigger Property="IsSelected" Value="true"> 
            <Setter TargetName="BorderItem" Property="Background" Value="{StaticResource Brush_Highlight}"/> 
           </Trigger> 
          </ControlTemplate.Triggers> 
         </ControlTemplate> 

        </Setter.Value> 
       </Setter> 
      </Style> 

     </Setter.Value> 
    </Setter> 

</Style> 
+0

請張貼在組合框和列表框(Style_ComboBoxColorPicker和Style_ListBox)中使用的樣式 – 2010-08-28 00:22:37

回答

0

我發現了這個問題。

我ItemContainerStyle模板有一個矩形,我想這意味着zindex莫名其妙。當我把它的內容繪製在邊界之前並且它是矩形時,它就起作用了。我只是不得不添加Panel.ZIndex =「1」,它的工作。

+0

我很高興你找到它。 – 2010-08-30 20:37:21

0

確保你沒有IsHitTestVisible =對組合框的風格 「假」。你可以在這裏發佈你使用的風格嗎?

+0

我試圖在組合框元素中設置IsHitTestVisible,並沒有工作。 – 2010-08-30 15:33:08