2016-11-02 68 views
0

以下幾個不同的答案插圖陰影我試着在這裏等我Textbox實現陰影效果,這裏是我的代碼至今:怎樣才能使用WPF XAML

<Style x:Key="TextBoxRoundedInset" 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="TextAlignment" Value="Center"/> 
      <Setter Property="HorizontalContentAlignment" Value="Center"/> 
      <Setter Property="VerticalContentAlignment" Value="Center"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type TextBox}"> 
         <Border x:Name="border" CornerRadius="3" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True" ClipToBounds="True"> 
          <Border Background="Transparent" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Margin="-2"> 
          <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/> 
          <Border.Effect> 
            <DropShadowEffect ShadowDepth="0" BlurRadius="10"/> 
           </Border.Effect> 
          </Border> 
         </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> 

然後顯示與造型我有

<TextBox x:Name="textBox" Background="#263238" Height="40" Margin="0,0,0,50" TextWrapping="Wrap" Width="221" VerticalAlignment="Center" HorizontalAlignment="Center" BorderThickness="0" Style="{DynamicResource TextBoxRoundedInset}" FontSize="25"/> 

但我最終的結果看起來像這樣:

Pic

我不希望文字發光,而是我基本上希望文本字段看起來像它掉到了背景任何信息如何糾正這將是非常感謝

回答

1

我通常使用BorderThickness屬性與BorderBrush屬性結合使用,可以使3D效果看起來像是在內部按下字段,或者甚至在外部通過交換兩個邊框的BorderThickness的值。

這是我用你的剪斷:

<Border x:Name="border" CornerRadius="3" BorderBrush="White" BorderThickness="0,0,2,2" Background="{TemplateBinding Background}" SnapsToDevicePixels="True" ClipToBounds="True"> 
    <Border Background="Transparent" BorderBrush="Black" BorderThickness="2,2,0,0" CornerRadius="2"> 
     <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/> 
    </Border> 
</Border> 

缺點是不能夠改變BorderBrushBorderThickness房產了,因爲他們沒有約束力。

只需改變這兩個邊框的顏色以適合您的需要,我通常只是使用黑白組合,因爲這種組合通常會產生最強烈的效果。

+0

感謝這實際上工作相當好,除非有人提出了一個不同的方法來做到這一點。儘管爲什麼輸入字段中的文本看起來很模糊,就像它有自己的陰影一樣? – Datsik

+0

對使用​​文本元素的對象使用DropShadowEffect的事情是,當沒有Background或Fill Property設置爲「透明」時,它也會在所述文本元素的內容上設置它的值。 –

+0

我需要在哪裏聲明背景透明? – Datsik