XAML / WPF焦点文本块

时间:2013-05-08 20:18:50

标签: wpf xaml

文本块是否可以集中在WPF中?我想改变文本块的背景颜色,如果它是当前的焦点,但我想在XAML中进行。 这就是我现在拥有的。它是Stackpanel中的一堆文本框。我可以让XAML定位到非焦点或基本状态,但是当我尝试添加触发器时,背景在焦点上不会改变。代码如下:

<Style x:Key="QueueListTextBlocks" TargetType="TextBlock">
            <Setter Property="Background" Value="#027802"></Setter>
            <Setter Property="Foreground" Value="White"></Setter>
            <Setter Property="Padding" Value="10,5"></Setter>
            <Setter Property="Margin" Value="5,2,5,0"></Setter>
            <Setter Property="FontSize" Value="14"></Setter>
            <Setter Property="Focusable" Value="true"/>
            <Setter Property="Cursor" Value="Hand"></Setter>  
            <!-- Trigger-->
            <Style.Triggers>
 <!--Does not pick up a IsFucused State--Alternative?-->

                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Background" Value="Blue"></Setter>
                    <Setter Property="FontSize" Value="18"></Setter>
                    <Setter Property="Foreground" Value="Orange"></Setter>
                </Trigger>

            </Style.Triggers>
            <!--<Setter Property="Background" Value="White" />-->
        </Style>

1 个答案:

答案 0 :(得分:0)

我尝试了你的风格,它完美无缺。 我的窗口中的TextBlocks只需按TAB键即可更改外观。 我正在使用.NET 4.0框架。

这是我窗口的XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <Style TargetType="TextBlock" x:Key="TextBlockStyle">
            <Setter Property="Background" Value="#027802"></Setter>
            <Setter Property="Foreground" Value="White"></Setter>
            <Setter Property="Padding" Value="10,5"></Setter>
            <Setter Property="Margin" Value="5,2,5,0"></Setter>
            <Setter Property="FontSize" Value="14"></Setter>
            <Setter Property="Focusable" Value="true"/>
            <Setter Property="Cursor" Value="Hand"></Setter>
            <!-- Trigger-->
            <Style.Triggers>
                <!--Does not pick up a IsFucused State-Alternative?-->

                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Background" Value="Blue"></Setter>
                    <Setter Property="FontSize" Value="18"></Setter>
                    <Setter Property="Foreground" Value="Orange"></Setter>
                </Trigger>

            </Style.Triggers>
            <!--<Setter Property="Background" Value="White" />-->
        </Style>
    </Window.Resources>

    <StackPanel Orientation="Vertical">
        <TextBlock Text="One" Style="{StaticResource TextBlockStyle}" />
        <TextBlock Text="Two" Style="{StaticResource TextBlockStyle}" />
        <TextBlock Text="Three" Style="{StaticResource TextBlockStyle}" />
        <TextBlock Text="Four" Style="{StaticResource TextBlockStyle}" />
        <TextBlock Text="Five" Style="{StaticResource TextBlockStyle}" />
    </StackPanel>
</Window>

我希望它有所帮助