2017-03-02 72 views
0

代碼chnage化背景色後失蹤所以這是我TextBox風格:Mahapps文本框風格通過背後

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource MetroTextBox}"> 
    <Setter Property="Foreground" Value="Gainsboro"/> 
    <Setter Property="BorderBrush" Value="Transparent"/> 
    <Setter Property="BorderThickness" Value="0"/> 
    <Setter Property="Controls:TextBoxHelper.ClearTextButton" Value="True"/> 
    <Setter Property="Padding" Value="0,1,0,0" /> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="Foreground" Value="White"/> 
      <Setter Property="Controls:TextBoxHelper.ClearTextButton" Value="True"/> 
     </Trigger> 
     <MultiTrigger> 
      <MultiTrigger.Conditions> 
       <Condition Property="IsEnabled" Value="False" /> 
       <Condition Property="IsMouseOver" Value="False"/> 
      </MultiTrigger.Conditions> 
      <MultiTrigger.Setters> 
       <Setter Property="Background" Value="#FF0052B2"/> 
       <Setter Property="BorderBrush" Value="Transparent"/> 
       <Setter Property="Foreground" Value="Gainsboro"/> 
      </MultiTrigger.Setters> 
     </MultiTrigger> 
     <MultiTrigger> 
      <MultiTrigger.Conditions> 
       <Condition Property="IsEnabled" Value="True" /> 
       <Condition Property="IsMouseOver" Value="False"/> 
      </MultiTrigger.Conditions> 
      <MultiTrigger.Setters> 
       <Setter Property="Foreground" Value="Gainsboro"/> 
       <Setter Property="Background" Value="#FF103766"/> 
       <Setter Property="BorderBrush" Value="Transparent"/> 
      </MultiTrigger.Setters> 
     </MultiTrigger> 
     <MultiTrigger> 
      <MultiTrigger.Conditions> 
       <Condition Property="IsEnabled" Value="True" /> 
       <Condition Property="IsMouseOver" Value="True"/> 
      </MultiTrigger.Conditions> 
      <MultiTrigger.Setters> 
       <Setter Property="Foreground" Value="White"/> 
       <Setter Property="Background" Value="#FF7AA0CD"/> 
       <Setter Property="BorderBrush" Value="White"/> 
      </MultiTrigger.Setters> 
     </MultiTrigger> 
    </Style.Triggers> 
</Style> 

我的控制器:

<TextBox 
    Name="Filter" 
    Width="398" 
    Height="25" 
    TextChanged="tbSnifferFilter_TextChanged"          
    Margin="23,0,0,0"/> 

在我的應用程序,用戶需要我TextBox過濾器裏面輸入(簡單的字符串),並通過以下方法我檢查此過濾器是否有效,並將顏色更改爲GreenRed

private void tbFilter_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    tbFilter.TextChanged -= tbFilter_TextChanged; 

    if (tbFilter.Text == "") // In case user click on clear button - return back to default backgroud color. 
    { 
     tbFilter.Background = (Brush)new BrushConverter().ConvertFromString("#FF103766"); 
     tbFilter.TextChanged += tbFilter_TextChanged; 
     return; 
    } 

    if (!IsSyntaxCurrect(tbFilter.Text)) 
     tbFilter.Background = Brushes.Salmon; 
    else 
     tbFilter.Background = Brushes.MediumSeaGreen; 

    tbFilter.TextChanged += tbFilter_TextChanged; 
} 

private bool IsSyntaxCurrect(string filter) 
{ 
    try 
    { 
     // Do my stuff... 
     return true; 
    } 
    catch (Exception) 
    { 
     return false; 
    } 
} 

因此添加後過濾器(有效OT無效),並在TextBox明確的按鈕,用戶點擊後清潔TextBoxBackground顏色恢復到默認的顏色,但Mouse Over沒有工作,什麼也不做。

回答

1

這是因爲您在事件處理程序中設置的本地Background值優先於您的Style設置的值。請參閱MSDN瞭解更多信息有關依賴項屬性值優先級:https://msdn.microsoft.com/en-us/library/ms743230(v=vs.110).aspx

取而代之的是Background屬性設置爲一個新的Brush值時TextBox被清除,你可以使用ClearValue方法清除依賴項屬性的值:

private void tbFilter_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    tbFilter.TextChanged -= tbFilter_TextChanged; 

    if (tbFilter.Text == "") // In case user click on clear button - return back to default backgroud color. 
    { 
     tbFilter.ClearValue(TextBox.BackgroundProperty); //<-- 
     tbFilter.TextChanged += tbFilter_TextChanged; 
     return; 
    } 

    if (!IsSyntaxCurrect(tbFilter.Text)) 
     tbFilter.Background = Brushes.Salmon; 
    else 
     tbFilter.Background = Brushes.MediumSeaGreen; 

    tbFilter.TextChanged += tbFilter_TextChanged; 
}