2013-07-03 25 views
2

這裏選擇整行是我的組合框的DataTemplate如何設置ComboBox項在WPF

<ComboBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel KeyboardNavigation.DirectionalNavigation="Contained" Orientation="Horizontal"> 
        <Image Source="{Binding Path=Flag}" Height="25"></Image> 
        <TextBlock Margin="10,0,0,0" Text="{Binding Path=Name}" VerticalAlignment="Center"></TextBlock> 
       </StackPanel> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
     <ComboBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <VirtualizingStackPanel/> 
      </ItemsPanelTemplate> 
     </ComboBox.ItemsPanel> 

它工作正常,我想選擇排當鼠標在任何地方都是行區域,而不是隻有當它僅僅是通過數據。

感謝enter image description here

確定,由@Jehof,感謝其解決。第二個問題是「如果我設置模板,爲什麼它不起作用?」 這樣

<Style TargetType="ComboBoxItem"> 
      <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
      <Setter Property="SnapsToDevicePixels" Value="true"/> 
      <!--<Setter Property="OverridesDefaultStyle" Value="true"/>--> 

      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ComboBoxItem"> 
         <Border Name="Border" Padding="2" SnapsToDevicePixels="False" BorderThickness="1"> 
          <ContentPresenter /> 
         </Border> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsHighlighted" Value="true"> 
           <Setter TargetName="Border" Property="Background" Value="{StaticResource BackgroundHighlighted}"/> 
           <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource BorderBrushHighlighted}"/> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

如果除去<Setter Property="Template"> ...部分它的作品!


@Jehof和爲什麼它不工作,如果我設置模板?

<Style TargetType="ComboBoxItem"> 
       <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
       <Setter Property="SnapsToDevicePixels" Value="true"/> 
       <!--<Setter Property="OverridesDefaultStyle" Value="true"/>--> 

       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="ComboBoxItem"> 
          <Border Name="Border" Padding="2" SnapsToDevicePixels="False" BorderThickness="1"> 
           <ContentPresenter /> 
          </Border> 
          <ControlTemplate.Triggers> 
           <Trigger Property="IsHighlighted" Value="true"> 
            <Setter TargetName="Border" Property="Background" Value="{StaticResource BackgroundHighlighted}"/> 
            <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource BorderBrushHighlighted}"/> 
           </Trigger> 
          </ControlTemplate.Triggers> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 

回答

4

添加ItemContainerStyle到您的組合框設置爲舒展了Horizo​​ntalContentAlignment。

<ComboBox.ItemContainerStyle> 
    <Style TargetType="ComboBoxItem"> 
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
    </Style > 
</ComboBox.ItemContainerStyle> 
+0

這是boshtunmay的工作,謝謝! –

+0

#Jehof請看我的帖子 –