2011-08-04 72 views
1

如何使ComboBox的選定項目出現在ComboBox文本字段中並使用Border s在ComboBoxControlTemplate?用下面的代碼的項目彈出不錯,但在選擇後ComboBox文本框從未露面;但是從ComboBox模板拆下2個Border小號修復此。爲什麼??怎麼樣??更重要的是:我怎麼使用這個模板,Border S和有SelectedItem選擇後的ComboBox文本框正確顯示?如何顯示選定的項目並使用組合框邊框?爲什麼會發生這種行爲?

<Window.Resources> 
    <Style x:Key="ComboboxDropdownButton" TargetType="{x:Type ToggleButton}"> 
     <Setter Property="MinWidth" Value="0"/> 
     <Setter Property="MinHeight" Value="0"/> 
     <Setter Property="Width" Value="NaN"/> 
     <Setter Property="Height" Value="NaN"/> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="BorderBrush" Value="Black"/> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ToggleButton}"> 
        <DockPanel SnapsToDevicePixels="True" 
           Background="{TemplateBinding Background}" 
           LastChildFill="False"> 
         <Border x:Name="Border" 
           Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" 
           DockPanel.Dock="Right" 
           Background="WhiteSmoke" 
           CornerRadius="0,3,3,0" 
           BorderThickness="{TemplateBinding BorderThickness}" 
           BorderBrush="{TemplateBinding BorderBrush}" 
           > 
          <Path Fill="{TemplateBinding Foreground}" 
            HorizontalAlignment="Center" VerticalAlignment="Center" Data="M0,0L4.5,4 9,0z"/> 
         </Border> 
        </DockPanel> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter TargetName="Border" Property="Background" Value="White" /> 
         </Trigger> 
         <Trigger Property="IsChecked" Value="True"> 
          <Setter TargetName="Border" Property="Background" Value="White" /> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="IsEnabled" Value="False"> 
       <Setter Property="Opacity" Value="0.5"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

    <ControlTemplate x:Key="ComboBoxTextBox" TargetType="{x:Type TextBox}"> 
     <Border x:Name="PART_ContentHost" Focusable="False" /> 
    </ControlTemplate> 

    <Style x:Key="{x:Type ComboBox}" 
      TargetType="{x:Type ComboBox}"> 
     <Setter Property="SnapsToDevicePixels" Value="true" /> 
     <Setter Property="OverridesDefaultStyle" Value="true" /> 
     <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" /> 
     <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" /> 
     <Setter Property="ScrollViewer.CanContentScroll" Value="true" /> 
     <Setter Property="MinWidth" Value="120" /> 
     <Setter Property="MinHeight" Value="20" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ComboBox}"> 
        <Border SnapsToDevicePixels="True" 
          x:Name="OuterBorder" 
          Background="Transparent" 
          BorderBrush="Red" 
          BorderThickness="1" 
          CornerRadius="4" 
          Margin="-1"> 
         <Border x:Name="InnerBorder" 
           Background="WhiteSmoke" 
           BorderThickness="1" 
           CornerRadius="3" 
           BorderBrush="Black"> 
          <Grid> 
           <VisualStateManager.VisualStateGroups> 
            <VisualStateGroup x:Name="EditStates"> 
             <VisualState x:Name="Editable"> 
              <Storyboard> 
               <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" 
                       Storyboard.TargetName="PART_EditableTextBox"> 
                <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" /> 
               </ObjectAnimationUsingKeyFrames> 
               <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" 
                       Storyboard.TargetName="ContentSite"> 
                <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Hidden}" /> 
               </ObjectAnimationUsingKeyFrames> 
              </Storyboard> 
             </VisualState> 
             <VisualState x:Name="Uneditable" /> 
            </VisualStateGroup> 
           </VisualStateManager.VisualStateGroups> 
           <ToggleButton x:Name="ToggleButton" 
               Margin="-1" 
               Grid.Column="2" 
               Focusable="False" 
               ClickMode="Press" 
               Style="{StaticResource ComboboxDropdownButton}" 
               IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"> 
           </ToggleButton> 
           <ContentPresenter x:Name="ContentSite" 
                IsHitTestVisible="False" 
                Content="{TemplateBinding SelectionBoxItem}" 
                ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
                ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 
                Margin="3,3,23,3" 
                VerticalAlignment="Stretch" 
                HorizontalAlignment="Left"> 
           </ContentPresenter> 
           <TextBox x:Name="PART_EditableTextBox" 
             Style="{x:Null}" 
             Template="{StaticResource ComboBoxTextBox}" 
             HorizontalAlignment="Left" 
             VerticalAlignment="Bottom" 
             Margin="3,3,23,3" 
             Focusable="True" 
             Background="Transparent" 
             Visibility="Hidden" 
             IsReadOnly="{TemplateBinding IsReadOnly}" /> 
           <Popup x:Name="PART_Popup" 
             Placement="Bottom" 
             IsOpen="{TemplateBinding IsDropDownOpen}" 
             AllowsTransparency="True" 
             Focusable="False" 
             PopupAnimation="Slide"> 
            <Grid x:Name="DropDown" 
              SnapsToDevicePixels="True" 
              MinWidth="{TemplateBinding ActualWidth}" 
              MaxHeight="{TemplateBinding MaxDropDownHeight}" 
              > 
             <Border x:Name="DropDownBorder" 
               MaxHeight="{TemplateBinding MaxDropDownHeight}" 
               MinWidth="{Binding ActualWidth, ElementName=Border}" 
               Background="WhiteSmoke" 
               BorderBrush="Black" 
               BorderThickness="1" CornerRadius="0,0,3,3"> 
             </Border> 
             <ScrollViewer Padding="1" SnapsToDevicePixels="True"> 
              <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" /> 
             </ScrollViewer> 
            </Grid> 
           </Popup> 
          </Grid> 
         </Border> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="HasItems" Value="false"> 
          <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95" /> 
         </Trigger> 
         <Trigger Property="IsGrouping" Value="true"> 
          <Setter Property="ScrollViewer.CanContentScroll" Value="false" /> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 

<Grid> 
    <ComboBox Width="120" Height="20" Name="comboBox1" SnapsToDevicePixels="True" 
       HorizontalAlignment="Center" VerticalAlignment="Center" 
       Text="ComboBox" SelectedIndex="0" IsEditable="True" IsReadOnly="True"> 
     <ComboBoxItem>item 1</ComboBoxItem> 
     <ComboBoxItem>item 2</ComboBoxItem> 
     <ComboBoxItem>item 3</ComboBoxItem> 
    </ComboBox> 
</Grid> 
+1

只是一個完全野生的猜測......我們曾經相同的情況下幾個月回來時,組合框彈出,並選擇不間歇地在我們與邊框定製組合框模板出現了,當我們從邊境刪除'CornerRadius'屬性它去了....: -/...我知道聽起來很愚蠢,完全荒謬,但那就是實際發生的事!它是一個謎我想不出解決,直到日期...你可以downvote我,如果這個「絕望」招行不通.... :-D –

+0

同樣奇怪的事情發生在我身上一次。也很奇怪:在.NET 3.0中,我們對TextBlock有一個DropShadowBitmap效果。它造成內存泄漏,導致我們的應用程序增加約1 MB /分鐘。 –

+0

是的!那個也是!我們的Splash屏幕上既有圓角,又有DropShadowBitmap,它上面的Combobox表演完全荒謬!當我們移除這兩個時,它開始工作正常! –

回答

0

我終於解決了這個問題..通過從模板中刪除邊框。我用模板的網格中的矩形替換了邊框。像魅力一樣工作。結果可以在下一個問題here中找到。

相關問題