把样式写到资源文件里,然后在FocusableChanged事件里调用相应的样式

解决方案 »

  1.   

    为了使颜色醒目,我用的是红和蓝
    Dictionary:    <Style x:Key="s1" TargetType="TextBox">
            <Setter Property="Background" Value="Red"></Setter>
        </Style>
        <Style x:Key="s2" TargetType="TextBox">
            <Setter Property="Background" Value="Blue"></Setter>
        </Style>
    xaml:<TextBox x:Name="txt" Width="100" Height="30" GotFocus="txt_GotFocus_1" LostFocus="txt_LostFocus_1" ></TextBox>
    cs:        private void txt_GotFocus_1(object sender, RoutedEventArgs e)
            {
                this.txt.Style = Resources["s1"] as Style;
            }        private void txt_LostFocus_1(object sender, RoutedEventArgs e)
            {
                this.txt.Style = Resources["s2"] as Style;
            }