2017-08-18 100 views
1

我試圖更改TextBoxIsMouseOver & IsFocusedBorderBrush。 但它似乎沒有影響,我總是得到這個醜陋的標準藍色。覆蓋xaml ui元素的默認Windows 10樣式

我的風格在ResourceDictionary之內。 它工作得很好,除了改變BorderBrush的問題。

<Style x:Key="TextBoxA" TargetType="TextBox" > 
    <Setter Property="Foreground" Value="#FFF0E6D2"/> 
    <Setter Property="FontSize" Value="15" /> 
    <Setter Property="BorderBrush" Value="#FF785A28"/> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    <Setter Property="Background" Value="#FF000306"/> 
    <Style.Triggers> 
     <Trigger Property="IsFocused" Value="True"> 
      <Setter Property="Background" > 
       <Setter.Value> 
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
         <GradientStop Color="#FF040B11" Offset="1"/> 
         <GradientStop Color="#FF11171B" Offset="0"/> 
        </LinearGradientBrush> 
       </Setter.Value> 
      </Setter> 
      <Setter Property="BorderBrush" > 
       <Setter.Value> 
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
         <GradientStop Color="#FF785A28" Offset="1"/> 
         <GradientStop Color="#FFC8C36E" Offset="0"/> 
        </LinearGradientBrush> 
       </Setter.Value> 
      </Setter> 
     </Trigger> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="BorderBrush" Value="#FF785A28"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

回答

1

您需要修改ControlTemplate。在TextBox設計模式在Visual Studio中單擊鼠標右鍵,然後選擇Edit Template - >Edit a Copy爲默認模板它複製到你的XAML標記,然後編輯按您的要求:

<TextBox> 
    <TextBox.Style> 
     <Style TargetType="{x:Type TextBox}"> 
      <Setter Property="Foreground" Value="#FFF0E6D2"/> 
      <Setter Property="FontSize" Value="15" /> 
      <Setter Property="BorderBrush" Value="#FF785A28"/> 
      <Setter Property="VerticalContentAlignment" Value="Center"/> 
      <Setter Property="Background" Value="#FF000306"/> 
      <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}" SnapsToDevicePixels="True"> 
          <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/> 
         </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="#FF785A28"/> 
          </Trigger> 
          <Trigger Property="IsKeyboardFocused" Value="true"> 
           <Setter Property="Background" > 
            <Setter.Value> 
             <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
              <GradientStop Color="#FF040B11" Offset="1"/> 
              <GradientStop Color="#FF11171B" Offset="0"/> 
             </LinearGradientBrush> 
            </Setter.Value> 
           </Setter> 
           <Setter Property="BorderBrush" > 
            <Setter.Value> 
             <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
              <GradientStop Color="#FF785A28" Offset="1"/> 
              <GradientStop Color="#FFC8C36E" Offset="0"/> 
             </LinearGradientBrush> 
            </Setter.Value> 
           </Setter> 
          </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.Style> 
</TextBox> 
+0

好了,把你的代碼樣式後在我的ResourceDictionary中,並將其應用於TextBox元素,該元素變得不可見。任何關於我做錯的建議? – Tom

+0

您是否嘗試將我的代碼原樣複製到新窗口中?嘗試將背景更改爲「黃色」或其他內容。 – mm8

+0

啊,我的錯......在編輯模板 - >編輯複製Visual Studio後,自動添加了一個模板綁定到我的文本框元素,並刪除了我的樣式綁定。無論如何,它似乎現在工作,但MouseOver和IsKeyboardFocused BorderBrush樣式不適用。 – Tom