2011-11-21 5 views
6

msdn 예제를 기반으로하는 콤보 상자 용 템플릿을 작성하려고하는데 이상한 오류가 발생했습니다. 좀 더 정확히 말하자면 보통 콤보 상자에 긴 텍스트를 쓴다면 텍스트가 왼쪽으로 이동하고 항상 캐럿을 보게됩니다. 그러나 긴 텍스트 캐럿을 썼을 때 msdn 예제에서는 콤보 외부로 이동합니다. Framework 3.5Framework 4 콤보 도움말에서 이후이 편집 가능한 ComboBox의 캐럿이 범위를 초과하는 이유는 무엇입니까?

enter image description here enter image description here

내가 사용한 예 : 나는 텍스트를 선택하면

enter image description here

그것은 다음과 같습니다 : 여기처럼 보이게하는 방법 picute입니다 복사 및 붙여 넣기 나는 같은 상황이 계속되고있다. 어떤 도움을 주셔서 감사합니다.

편집 :

<Window x:Class="ComboBoxTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Framework 3.5" Height="350" Width="525"> 

    <Window.Resources> 
     <ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton"> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition /> 
        <ColumnDefinition Width="20" /> 
       </Grid.ColumnDefinitions> 
       <Border x:Name="Border" Grid.ColumnSpan="2" CornerRadius="2" Background="White" 
         BorderBrush="Black" BorderThickness="1" /> 
       <Border Grid.Column="0" CornerRadius="2,0,0,2" Margin="1" Background="#FFF" 
         BorderBrush="Black" BorderThickness="0,0,1,0" /> 
       <Path x:Name="Arrow" Grid.Column="1" Fill="#444" HorizontalAlignment="Center" 
         VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z" /> 
      </Grid> 
      <ControlTemplate.Triggers> 
       <Trigger Property="ToggleButton.IsMouseOver" Value="true"> 
        <Setter TargetName="Border" Property="Background" Value="Black" /> 
       </Trigger> 
       <Trigger Property="ToggleButton.IsChecked" Value="true"> 
        <Setter TargetName="Border" Property="Background" Value="LightGray" /> 
       </Trigger> 
       <Trigger Property="IsEnabled" Value="False"> 
        <Setter TargetName="Border" Property="Background" Value="#EEE" /> 
        <Setter TargetName="Border" Property="BorderBrush" Value="#AAA" /> 
        <Setter Property="Foreground" Value="#888" /> 
        <Setter TargetName="Arrow" Property="Fill" Value="#888" /> 
       </Trigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 

     <!-- something is wrong here but what?? --> 
     <ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox"> 
      <Border x:Name="PART_ContentHost" Focusable="False" /> 
     </ControlTemplate> 

     <Style x:Key="MsComboBox" TargetType="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="ComboBox"> 
         <Grid> 
          <ToggleButton Name="ToggleButton" Template="{StaticResource ComboBoxToggleButton}" Grid.Column="2" 
              Focusable="false" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" 
              ClickMode="Press"></ToggleButton> 

          <ContentPresenter Name="ContentSite" IsHitTestVisible="False" Content="{TemplateBinding SelectionBoxItem}" 
               ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
               ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="3,3,23,3" VerticalAlignment="Center" 
               HorizontalAlignment="Left" /> 

          <TextBox x:Name="PART_EditableTextBox" 
            Style="{x:Null}" 
            Template="{StaticResource ComboBoxTextBox}" 
            HorizontalAlignment="Left" VerticalAlignment="Center" Margin="3,3,23,3" Focusable="True" Background="Transparent" 
            Visibility="Hidden" IsReadOnly="{TemplateBinding IsReadOnly}" /> 

          <Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" 
            Focusable="False" PopupAnimation="Slide"> 
           <Grid Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" 
             MaxHeight="{TemplateBinding MaxDropDownHeight}"> 
            <Border x:Name="DropDownBorder" Background="Black" BorderThickness="1" 
              BorderBrush="#888" /> 
            <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True"> 
             <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" /> 
            </ScrollViewer> 
           </Grid> 
          </Popup> 
         </Grid> 
         <ControlTemplate.Triggers>        
          <Trigger Property="IsEditable" Value="true"> 
           <Setter Property="IsTabStop" Value="false" /> 
           <Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible" /> 
           <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden" /> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Window.Resources> 

    <Grid> 
     <ComboBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="comboBox1" VerticalAlignment="Top" IsEditable="True" 
        Width="185" Style="{StaticResource MsComboBox}" /> 
     <ComboBox Height="23" HorizontalAlignment="Left" Margin="12,41,0,0" Name="comboBox2" VerticalAlignment="Top" IsEditable="True" Width="185" /> 

    </Grid> 
</Window> 
+1

귀하의 콤보 박스 용 XAML을 게시하십시오. – Paparazzi

+0

그래서 우리는 스스로를 읽지 않고 어딘가에 복사하여 붙여 넣은 진절머리 나는 XAML을 디버그하기를 원하십니까? –

+1

Heh. 내 코드를 게시 하겠지만 MSDN의 코드와 길고 매우 유사합니다. 그리고 그것은 MSDN 공식적인 페이지에서 진절머리 나는 XAML이 아니라 XAML입니다. 나는이 예제로 콤보 박스 템플릿을 검색하는 모든 사람들이 어쩌면 그들에게 잘못된 것이 무엇인지, 누군가가 더 나은 템플릿 예제를 알고 있다고 생각한다고 생각한다. 나는 왜 텍스트 박스가 그렇게 행동하지 않는지 모르겠다. – bizon

답변

6

귀하의 Border가 화면 밖으로 간다, 그래서 그냥 그리기 유지하면 자동으로 컨텐츠를 스크롤하기위한 기능을 포함하지 않습니다. ScrollViewer으로 변경하면 제대로 작동합니다.

<ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox"> 
    <ScrollViewer x:Name="PART_ContentHost" Focusable="False" /> 
</ControlTemplate> 
관련 문제