1

文本块在 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>
4

1 回答 1

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>

我希望它有帮助

于 2014-12-29T17:18:04.130 回答