2011-11-21 11 views
6

私はmsdnの例に基づいてコンボボックスのテンプレートを構築しようとしていますが、私は奇妙なエラーがあります。もっと正確に言えば、通常のコンボボックスで長いテキストを書いた場合、テキストは左に移動し、常にキャレットが表示されます。しかし、msdnの例では、長いテキストを書きましたが、キャレットはコンボ以外に移動しています。この編集可能なComboBoxのキャレットが境界を超えているのはなぜですか?

enter image description here enter image description here

私は Framework 3.5Framework 4コンボボックスのヘルプからと後の例を使用している:私はそれがこのように見えるテキストを選択すると

enter image description here

:ここではそれがどのように見えるかpicuteですコピー&ペースト私は同じ状況を続けています。助けてくれてありがとう。

EDIT:

<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> 
関連する問題