2009-07-02 78 views
3

這讓我發瘋。我似乎無法獲得我的ComboBox中的數據模板來拉伸下拉的寬度。是什麼賦予了?在Silverlight中拉伸組合框內容

<Grid x:Name="LayoutRoot" Background="White"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="30" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <ComboBox x:Name="SearchesComboBox" HorizontalContentAlignment="Stretch" Width="150"> 
     <ComboBox.ItemContainerStyle> 
      <Style TargetType="ComboBoxItem"> 
       <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
      </Style> 
     </ComboBox.ItemContainerStyle> 
     <ComboBox.ItemTemplate> 
      <DataTemplate> 
       <Border BorderBrush="Black" BorderThickness="1"> 
        <TextBlock Text="{Binding}" Margin="2" /> 
       </Border> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
     <sys:String>Two</sys:String> 
     <sys:String>Four</sys:String> 
     <sys:String>Six</sys:String> 
    </ComboBox> 
</Grid> 

回答

2

好吧,經過大約一天的擺弄,我有一個解決方案。我恨它,但它工作。基本上,我反映到Silverlight的System.Windows.dll中,並且爲ListBoxItem(ComboBoxItem使用)剔除了默認模板。

事實證明,在該模板中,有一個ContentPresenter,其中Horizo​​ntalAlignment硬編碼爲Left。所以,我撕掉了模板,併爲Horizo​​ntalAlignment添加了一個TemplateBinding,因此它可以使用ComboBoxItem的Horizo​​ntalAlignment。

這就是說,接下來是工作代碼。如果有人有更好的方式來做到這一點,請告訴我。我還沒有檢查,如果這是固定在3.0。我希望是。它應該從ComboBox.Horizo​​ntalContentAlignment一路綁定。

<UserControl.Resources> 
    <Style x:Key="FixedComboBoxItem" TargetType="ComboBoxItem"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ComboBoxItem"> 
        <Grid Background="{TemplateBinding Background}"> 
         <vsm:VisualStateManager.VisualStateGroups> 
          <vsm:VisualStateGroup x:Name="CommonStates"> 
           <vsm:VisualState x:Name="Normal" /> 
           <vsm:VisualState x:Name="MouseOver"> 
            <Storyboard> 
             <DoubleAnimation Storyboard.TargetName="fillColor" Storyboard.TargetProperty="Opacity" Duration="0" To=".35"/> 
            </Storyboard> 
           </vsm:VisualState> 
           <vsm:VisualState x:Name="Disabled"> 
            <Storyboard> 
             <DoubleAnimation Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="Opacity" Duration="0" To=".55" /> 
            </Storyboard> 
           </vsm:VisualState> 
          </vsm:VisualStateGroup> 
          <vsm:VisualStateGroup x:Name="SelectionStates"> 
           <vsm:VisualState x:Name="Unselected" /> 
           <vsm:VisualState x:Name="Selected"> 
            <Storyboard> 
             <DoubleAnimation Storyboard.TargetName="fillColor2" Storyboard.TargetProperty="Opacity" Duration="0" To=".75"/> 
            </Storyboard> 
           </vsm:VisualState> 
          </vsm:VisualStateGroup> 
          <vsm:VisualStateGroup x:Name="FocusStates"> 
           <vsm:VisualState x:Name="Focused"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Visibility" Duration="0"> 
              <DiscreteObjectKeyFrame KeyTime="0"> 
               <DiscreteObjectKeyFrame.Value> 
                <Visibility>Visible</Visibility> 
               </DiscreteObjectKeyFrame.Value> 
              </DiscreteObjectKeyFrame> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </vsm:VisualState> 
           <vsm:VisualState x:Name="Unfocused"/> 
          </vsm:VisualStateGroup> 
         </vsm:VisualStateManager.VisualStateGroups> 
         <Rectangle x:Name="fillColor" Opacity="0" Fill="#FFBADDE9" IsHitTestVisible="False" RadiusX="1" RadiusY="1"/> 
         <Rectangle x:Name="fillColor2" Opacity="0" Fill="#FFBADDE9" IsHitTestVisible="False" RadiusX="1" RadiusY="1"/> 
         <ContentPresenter 
          x:Name="contentPresenter" 
          Content="{TemplateBinding Content}" 
          ContentTemplate="{TemplateBinding ContentTemplate}" 
          HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 
          Margin="{TemplateBinding Padding}"/> 
         <Rectangle x:Name="FocusVisualElement" Stroke="#FF6DBDD1" StrokeThickness="1" Visibility="Collapsed" RadiusX="1" RadiusY="1" /> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</UserControl.Resources> 

<Grid x:Name="LayoutRoot" Background="White"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="30" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <ComboBox x:Name="SearchesComboBox" Width="150" ItemContainerStyle="{StaticResource FixedComboBoxItem}" HorizontalContentAlignment="Stretch"> 
     <ComboBox.ItemTemplate> 
      <DataTemplate> 
       <Border BorderBrush="Black" BorderThickness="1" > 
        <TextBlock Text="{Binding}" Margin="2" /> 
       </Border> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
     <sys:String>Two</sys:String> 
     <sys:String>Four</sys:String> 
     <sys:String>Six</sys:String> 
    </ComboBox> 
</Grid> 
2

我剛剛確認了以下工作在WPF,同比移動Horizo​​ntalContentAlignment到ComboBox:

<ComboBox x:Name="SearchesComboBox" HorizontalContentAlignment="Stretch" Width="150"> 
    <ComboBox.ItemTemplate> 
    <DataTemplate> 
     <Border BorderBrush="Black" BorderThickness="1"> 
     <TextBlock Text="{Binding}" Margin="2" /> 
     </Border> 
    </DataTemplate> 
    </ComboBox.ItemTemplate> 
    <sys:String>Two</sys:String> 
    <sys:String>Four</sys:String> 
    <sys:String>Six</sys:String> 
</ComboBox> 

讓我知道,如果這也Silverlight中修復該問題。我看到的問題是下拉菜單中的項目大小正確,而下拉菜單中顯示的內容沒有擴展。

+0

不,不幸的是,這並沒有在Silverlight中工作。下拉菜單中的項目大小不正確,但標題中的內容大小正確。驅使我瘋了:) – 2009-07-02 14:21:44

+0

剛做了Silverlight 5的測試,這種方法爲我解決了這個問題。 – Rus 2012-06-20 10:37:32

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