2017-06-14 130 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 ......如此簡單,我沒有找到它... –

相關問題