通过隐藏代码更改背景颜色后,Mahapps TextBox 样式丢失

Mahapps TextBox style missing after chnage Backgroud color via code behind

所以这是我的 TextBox 风格:

<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 过滤器(简单字符串)中输入,并通过以下方法检查此过滤器是否有效并更改我的 TextBox Backgroud 颜色到 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;
    }
}

所以在添加过滤器(有效或无效)之后,用户点击 TextBox 清除按钮将 TextBox Background 颜色 return 清除到默认颜色,但 Mouse Over 不起作用并且什么都不做。

这是因为您在事件处理程序中设置的本地 Background 值优先于您 Style 设置的值。有关依赖项 属性 值优先级的更多信息,请参阅 MSDN:https://msdn.microsoft.com/en-us/library/ms743230(v=vs.110).aspx

在清除 TextBox 时,不是将 Background 属性 设置为新的 Brush 值,而是清除依赖项的值 属性 使用 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;
}