2017-06-14 220 views
0

我尝试为组合框执行模板。 ItemTemplate中是确定的,但不是选择的项目如u可以看到如下:组合框WPF模板项目和选定项目

enter image description here

我的代码:

   <ComboBox ItemsSource="{Binding CouleursList}" 
         SelectedItem="{Binding SelectedCouleur}" 
         Grid.Column="1" Grid.Row="2"> 
       <ComboBox.ItemContainerStyle> 
        <Style TargetType="{x:Type ComboBoxItem}"> 
         <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
        </Style> 
       </ComboBox.ItemContainerStyle> 
       <ComboBox.ItemTemplate> 
        <DataTemplate> 
         <Grid> 
          <Rectangle Stroke="Black" Margin="1" Height="15" 
             HorizontalAlignment="Stretch"> 
           <Rectangle.Fill> 
            <SolidColorBrush Color="{Binding Path=., Converter={StaticResource ColorConverter}}"/> 
           </Rectangle.Fill> 
          </Rectangle> 
         </Grid> 
        </DataTemplate> 
       </ComboBox.ItemTemplate> 
      </ComboBox> 

如果我给了一个固定的宽度,即工作,但矩形是一致的左和中心的名单

谢谢!

回答

0

所选项目内容未显示在ComboBoxItem中,因此您的HorizontalContentAlignment样式设置器不适用。

您可以设置该属性的ComboBox本身,但是:

<ComboBox 
    ItemsSource="{Binding CouleursList}" 
    SelectedItem="{Binding SelectedCouleur}" 
    HorizontalContentAlignment="Stretch" 
+0

OMG ......如此简单,我没有找到它... –

相关问题