1
votes

Are textblocks focusable in WPF? I want to change the background color of the textblock if it is currently the one focused, but I want to do it in XAML. This is what I have now. It is a bunch of textboxes in a Stackpanel. I can get the XAML to target the non focus or base state, but when I try to add a trigger, the background does not change on focus. Code is below:

<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
stackoverflow.com/questions/3351519/… . please look at this answer.Gilad
When I do this, it states that the Property="Template" is not recognized or accessibleClosDesign

1 Answers

0
votes

I tried your style and it works perfectly. TextBlocks in my window change their look just pressing the TAB key. I'm using .NET 4.0 framework.

This is the XAML of my window:

<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>

I hope it helps