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
過濾器裏面輸入(簡單的字符串),並通過以下方法我檢查此過濾器是否有效,並將顏色更改爲Green
或Red
:
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
明確的按鈕,用戶點擊後清潔TextBox
的Background
顏色恢復到默認的顏色,但Mouse Over
沒有工作,什麼也不做。