2017-01-17 5 views
0

私のコントロールのサイズがTextBoxを変更したり、フォントが正しく表示されるようにTextBoxをスタイルします。コントロールがサイズ変更されたときにフォントが正しく表示されるようにTextBoxのサイズを変更します。

私はほとんど働いています。

私のスタイル:

<UserControl.Resources> 
    <Style x:Key="ViewBoxTextBox" TargetType="TextBox"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="TextBox"> 
        <Viewbox HorizontalAlignment="Left"> 
         <TextBox Text="{TemplateBinding Text}" Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualWidth}"/> 
        </Viewbox> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</UserControl.Resources> 

コード例:以下は

<TextBox Style="{StaticResource ViewBoxTextBox}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Margin="2" Text="{Binding MyText}"/> 

は、3つの条件のスクリーンショットです。第三のスクリーンショットで

Screenshots

私は私のコントロールの高さを低くするときのTextBoxフォントが正しく大きさで減少しているが、テキストボックスの幅が減少し、全体の利用可能幅を占有されていません。

アイデア?

+0

ViewBoxのStretchプロパティをFillに設定してください。 – mm8

+0

@ mm8幅を最大に固定することはできますが、フォントサイズがテキストボックス内で減少するのを防ぎました。 – CathalMF

+0

最も簡単なルートは、 'ViewBox'でそれを叩く –

答えて

0

コントロールのテンプレートの基本的なオーバーライドを行うことがありますが、この場合、TextBox内のテキストをより詳細に制御する必要があります。これを行うために、私はTextBoxのデフォルトのテンプレートのコピーを得て、次にいくつかの変更を加えました。

<SolidColorBrush x:Key="TextBox.Static.Border" Color="#FFABAdB3"/> 
    <SolidColorBrush x:Key="TextBox.MouseOver.Border" Color="#FF7EB4EA"/> 
    <SolidColorBrush x:Key="TextBox.Focus.Border" Color="#FF569DE5"/> 
    <Style x:Key="ViewBoxTextBox" TargetType="{x:Type TextBox}"> 
     <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> 
     <Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border}"/> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/> 
     <Setter Property="HorizontalContentAlignment" Value="Left"/> 
     <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
     <Setter Property="AllowDrop" Value="true"/> 
     <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/> 
     <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type TextBox}"> 
        <Border x:Name="border" 
          BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" 
          Background="{TemplateBinding Background}" 
          VerticalAlignment="Top" 
          Width="{Binding RelativeSource={RelativeSource TemplatedParent},Path=ActualWidth}" 
          SnapsToDevicePixels="True"> 
         <Viewbox HorizontalAlignment="Left" StretchDirection="DownOnly"> 
          <ScrollViewer x:Name="PART_ContentHost" 
              Focusable="false" 
              HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" 
              Width="{Binding RelativeSource={RelativeSource TemplatedParent},Path=ActualWidth}"/> 
         </Viewbox> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Opacity" TargetName="border" Value="0.56"/> 
         </Trigger> 
         <Trigger Property="IsMouseOver" Value="true"> 
          <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.MouseOver.Border}"/> 
         </Trigger> 
         <Trigger Property="IsKeyboardFocused" Value="true"> 
          <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.Focus.Border}"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <MultiTrigger> 
       <MultiTrigger.Conditions> 
        <Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/> 
        <Condition Property="IsSelectionActive" Value="false"/> 
       </MultiTrigger.Conditions> 
       <Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/> 
      </MultiTrigger> 
     </Style.Triggers> 
    </Style> 

ContentHostは、テキストを正しくスケールするViewBox内にラップされ、より一般的なサイズの制約に基づいてスケーリングされます。私はいくつかの異なるシナリオの下でそれを試してみました。うまくいけばそれはあなたの使用のために働く。

関連する問題